OSDN Git Service

03d336a633893b8cf2b4dbbc5b3f8589faf1c321
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 00_XNet.js
1 // TODO  onlineevent offlineevent, netspeed\r
2 // local への通信に対しては、netspeed を更新しない\r
3 \r
4 // http://bugs.jquery.com/ticket/2709\r
5 // <head><base> がある場合、<script>の追加に失敗する\r
6 X.Net = {\r
7 \r
8         xhrGet : function( url, type ){\r
9                 return new X_NET_Queue( X_NET_TYPE_XHR, { method : 'GET', url : url, type : type } );\r
10         },\r
11         \r
12         xhrPost : function( url, type, postbody ){\r
13                 return new X_NET_Queue( X_NET_TYPE_XHR, { method : 'POST', url : url, type : type, postbody : postbody } );\r
14         },\r
15         \r
16         formGet : function(){\r
17                 \r
18         },\r
19         \r
20         formPost : function(){\r
21                 \r
22         },\r
23         \r
24         // TODO chashe\r
25         // TODO iframe useful or not. TODO dynamicIframe\r
26         // TODO file: では http: は使えない\r
27         jsonp : function( url, useIframe ){\r
28                 return new X_NET_Queue( X_NET_TYPE_JSONP, url );\r
29         },\r
30         \r
31         image : function( url, needSize, delay, timeout ){\r
32                 return new X_NET_Queue( X_NET_TYPE_IMAGE, { url : url, needSize : needSize } );\r
33         },\r
34         \r
35         // <script>, <link>\r
36         \r
37         amountQueue : function(){\r
38                 return X_NET_QUEUE_LIST.length;\r
39         }\r
40 \r
41 };\r
42 \r
43 /*\r
44  * @interface\r
45  */\r
46 var X_NET_IWrapper = function(){};\r
47         X_NET_IWrapper.prototype.load   = function(){};\r
48         X_NET_IWrapper.prototype.cancel = function(){};\r
49         X_NET_IWrapper.prototype.reset  = function(){};\r
50 \r
51 \r
52 var X_NET_TYPE_XHR   = 1,\r
53         X_NET_TYPE_JSONP = 2,\r
54         X_NET_TYPE_FORM  = 3,\r
55         X_NET_TYPE_IMAGE = 4,\r
56 \r
57         X_NET_QUEUE_LIST = [],\r
58 \r
59         X_NET_XHRWrapper,\r
60         X_NET_JSONPWrapper,\r
61         X_NET_FormWrapper,\r
62         X_NET_ImageWrapper,\r
63 \r
64         X_NET_currentWrapper,\r
65         X_NET_currentQueue,\r
66 \r
67         X_NET_Queue = X.EventDispatcher.inherits(\r
68                 'XNetQueue',\r
69                 X.Class.POOL_OBJECT,\r
70                 {\r
71                         type : 0,\r
72                         data : null,\r
73                         \r
74                         Constructor : function( type, data ){\r
75                                 this.type = type;\r
76                                 this.data = data;                               \r
77                                 \r
78                                 //this.listenOnce( X_Event.COMPLETE, X_NET_proxyDispatch );\r
79                                 X_EventDispatcher_systemListen( this, X_Event.COMPLETE, X_NET_proxyDispatch );\r
80                                 \r
81                                 X_NET_QUEUE_LIST[ X_NET_QUEUE_LIST.length ] = this;\r
82                                 !X_NET_currentQueue && X_NET_shiftQueue();\r
83                         },\r
84                         \r
85                         busy : function(){\r
86                                 return this === X_NET_currentQueue && X_NET_currentWrapper._busy;\r
87                         },\r
88                         \r
89                         cancel : function(){\r
90                                 var i = X_NET_QUEUE_LIST.indexOf( this );\r
91                                 if( i !== -1 ){\r
92                                         X_NET_QUEUE_LIST.splice( i, 1 );\r
93                                         this.asyncDispatch( X_Event.CANCELED );\r
94                                         this.asyncDispatch( 16, X_Event.COMPLETE );\r
95                                 } else\r
96                                 if( this === X_NET_currentQueue ){\r
97                                         X_NET_currentWrapper.cancel();\r
98                                 };\r
99                         }\r
100                 }\r
101         );\r
102 \r
103 function X_NET_proxyDispatch( e ){\r
104         switch( e.type ){\r
105                 case X_Event.BEFORE_KILL_INSTANCE :\r
106                         break;\r
107                 case X_Event.KILL_INSTANCE :\r
108                         break;                  \r
109                 case X_Event.KILL_INSTANCE_CANCELED :\r
110                         break;\r
111                 case X_Event.PROGRESS :\r
112                         this.dispatch( e );\r
113                         break;\r
114                 case X_Event.SUCCESS :\r
115                 case X_Event.ERROR :\r
116                 case X_Event.TIMEOUT :\r
117                 case X_Event.CANCELED :\r
118                         this.dispatch( e );\r
119                         this.asyncDispatch( X_Event.COMPLETE );\r
120                         break;\r
121                 case X_Event.COMPLETE :\r
122                         this.kill();\r
123                         X_NET_shiftQueue();\r
124                         break;\r
125         };\r
126 };\r
127 \r
128 function X_NET_shiftQueue(){\r
129         var queue;\r
130         \r
131         if( X_NET_currentQueue ){\r
132                 console.log( 'X_NET_shiftQueue ' + X_NET_currentWrapper._busy );\r
133                 if( X_NET_currentWrapper._busy ) return;\r
134                 X_NET_currentWrapper\r
135                         .unlisten( [ X_Event.PROGRESS, X_Event.SUCCESS, X_Event.ERROR, X_Event.TIMEOUT, X_Event.CANCELED ], X_NET_currentQueue, X_NET_proxyDispatch )\r
136                         .reset();\r
137                 X_NET_currentQueue = X_NET_currentWrapper = null;\r
138         };\r
139         \r
140         if( !X_NET_QUEUE_LIST.length ) return;\r
141 \r
142         queue = X_NET_QUEUE_LIST.shift();\r
143         \r
144         switch( queue.type ){\r
145                 case X_NET_TYPE_XHR :\r
146                         X_NET_currentWrapper = X_NET_XHRWrapper;\r
147                         break;\r
148                 case X_NET_TYPE_JSONP :\r
149                         X_NET_currentWrapper = X_NET_JSONPWrapper;\r
150                         break;\r
151                 case X_NET_TYPE_FORM :\r
152                         X_NET_currentWrapper = X_NET_FormWrapper;\r
153                         break;\r
154                 case X_NET_TYPE_IMAGE :\r
155                         X_NET_currentWrapper = X_NET_ImageWrapper;\r
156                         break;\r
157         };\r
158         \r
159         X_NET_currentWrapper.listen( [ X_Event.PROGRESS, X_Event.SUCCESS, X_Event.ERROR, X_Event.TIMEOUT, X_Event.CANCELED ], X_NET_currentQueue = queue, X_NET_proxyDispatch );\r
160         \r
161         X_NET_currentWrapper.load( queue.data );\r
162 };\r
163 \r