OSDN Git Service

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