]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/direct/PollingProvider.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / direct / PollingProvider.js
CommitLineData
6527f429
DM
1/**\r
2 * Provides for repetitive polling of the server at distinct {@link #interval intervals}.\r
3 * The initial request for data originates from the client, and then is responded to by the\r
4 * server.\r
5 * \r
6 * Configuration for the PollingProvider can be generated by the server-side\r
7 * API portion of the Ext Direct stack.\r
8 *\r
9 * An instance of PollingProvider may be created directly via the new keyword or by simply\r
10 * specifying `type = 'polling'`. For example:\r
11 *\r
12 * var pollA = new Ext.direct.PollingProvider({\r
13 * type:'polling',\r
14 * url: 'php/pollA.php',\r
15 * });\r
16 * Ext.direct.Manager.addProvider(pollA);\r
17 * pollA.disconnect();\r
18 * \r
19 * Ext.direct.Manager.addProvider({\r
20 * type:'polling',\r
21 * url: 'php/pollB.php',\r
22 * id: 'pollB-provider'\r
23 * });\r
24 * var pollB = Ext.direct.Manager.getProvider('pollB-provider');\r
25 *\r
26 */\r
27Ext.define('Ext.direct.PollingProvider', {\r
28 extend: 'Ext.direct.JsonProvider',\r
29 alias: 'direct.pollingprovider',\r
30 \r
31 requires: [\r
32 'Ext.Ajax',\r
33 'Ext.util.TaskRunner',\r
34 'Ext.direct.ExceptionEvent'\r
35 ],\r
36 \r
37 type: 'polling',\r
38 \r
39 /**\r
40 * @cfg {Number} [interval=3000]\r
41 * How often to poll the server-side in milliseconds. Defaults to every 3 seconds.\r
42 */\r
43 interval: 3000,\r
44\r
45 /**\r
46 * @cfg {Object} [baseParams]\r
47 * An object containing properties which are to be sent as parameters on every\r
48 * polling request. Note that if baseParams are set and {@link #url} parameter\r
49 * is an URL string, poll requests will use POST method instead of default GET.\r
50 */\r
51 \r
52 /**\r
53 * @cfg {String/Function} url\r
54 * The url which the PollingProvider should contact with each request. This can also be\r
55 * an imported Ext Direct method which will be passed baseParams as named arguments.\r
56 *\r
57 * *Note* that using string `url` is deprecated, use {@link #pollFn} instead.\r
58 * @deprecated 5.1.0\r
59 */\r
60 \r
61 /**\r
62 * @cfg {String/Function} pollFn\r
63 *\r
64 * Ext Direct method to use for polling. If a method name is provided as a string,\r
65 * the actual function will not be resolved until the first time this provider\r
66 * is connected.\r
67 *\r
68 * The method should accept named arguments and will be passed {@link #baseParams}\r
69 * if set.\r
70 */\r
71 \r
72 /**\r
73 * @event beforepoll\r
74 * @preventable\r
75 * Fired immediately before a poll takes place.\r
76 *\r
77 * @param {Ext.direct.PollingProvider} this\r
78 */\r
79\r
80 /**\r
81 * @event poll\r
82 * Fired immediately after a poll takes place.\r
83 *\r
84 * @param {Ext.direct.PollingProvider} this\r
85 */\r
86 \r
87 constructor: function(config) {\r
88 var me = this;\r
89 \r
90 me.callParent([config]);\r
91 \r
92 me.pollTask = Ext.TaskManager.newTask({\r
93 run: me.runPoll,\r
94 interval: me.interval,\r
95 scope: me\r
96 });\r
97 },\r
98 \r
99 destroy: function() {\r
100 this.pollTask = null;\r
101 \r
102 this.callParent();\r
103 },\r
104 \r
105 doConnect: function() {\r
106 var me = this,\r
107 url = me.url,\r
108 pollFn = me.pollFn;\r
109 \r
110 // It is important that pollFn resolution happens at the time when\r
111 // Provider is first connected, and not at construction time. If\r
112 // pollFn is configured as a string, the API stub may not exist yet\r
113 // when PollingProvider is constructed.\r
114 if (pollFn && Ext.isString(pollFn)) {\r
115 //<debug>\r
116 var fnName = pollFn;\r
117 //</debug>\r
118 \r
119 me.pollFn = pollFn = Ext.direct.Manager.parseMethod(pollFn);\r
120 \r
121 //<debug>\r
122 if (!Ext.isFunction(pollFn)) {\r
123 Ext.raise("Cannot resolve Ext Direct API method " + fnName +\r
124 " for PollingProvider");\r
125 }\r
126 //</debug>\r
127 }\r
128 else if (Ext.isFunction(url)) {\r
129 //<debug>\r
130 Ext.log.warn('Using a function for url is deprecated, use pollFn instead.');\r
131 //</debug>\r
132 \r
133 me.pollFn = pollFn = url;\r
134 me.url = url = null;\r
135 }\r
136 \r
137 if (url || pollFn) {\r
138 me.setInterval(me.interval);\r
139 \r
140 me.pollTask.start();\r
141 }\r
142 },\r
143\r
144 doDisconnect: function() {\r
145 this.pollTask.stop();\r
146 },\r
147 \r
148 getInterval: function() {\r
149 return this.pollTask.interval;\r
150 },\r
151 \r
152 setInterval: function(interval) {\r
153 var me = this,\r
154 pollTask = me.pollTask;\r
155 \r
156 //<debug>\r
157 if (interval < 100) {\r
158 Ext.raise("Attempting to configure PollProvider " + me.id +\r
159 " with interval that is less than 100ms.");\r
160 \r
161 }\r
162 //</debug>\r
163 \r
164 me.interval = pollTask.interval = interval;\r
165 \r
166 if (me.isConnected()) {\r
167 pollTask.restart(interval);\r
168 }\r
169 },\r
170 \r
171 /**\r
172 * @private\r
173 */\r
174 runPoll: function() {\r
175 var me = this,\r
176 url = me.url,\r
177 pollFn = me.pollFn,\r
178 baseParams = me.baseParams,\r
179 args;\r
180 \r
181 if (me.fireEvent('beforepoll', me) !== false) {\r
182 if (pollFn) {\r
183 args = pollFn.directCfg.method.getArgs({\r
184 params: baseParams !== undefined ? baseParams : {},\r
185 callback: me.onPollFn,\r
186 scope: me\r
187 });\r
188 \r
189 pollFn.apply(window, args);\r
190 }\r
191 else {\r
192 Ext.Ajax.request({\r
193 url: url,\r
194 callback: me.onData,\r
195 scope: me,\r
196 params: baseParams\r
197 });\r
198 }\r
199 \r
200 me.fireEvent('poll', me);\r
201 }\r
202 },\r
203\r
204 /**\r
205 * @private\r
206 */\r
207 onData: function(opt, success, response) {\r
208 var me = this, \r
209 i, len, events;\r
210 \r
211 if (success) {\r
212 events = me.createEvents(response);\r
213 \r
214 for (i = 0, len = events.length; i < len; ++i) {\r
215 me.fireEvent('data', me, events[i]);\r
216 }\r
217 }\r
218 else {\r
219 events = new Ext.direct.ExceptionEvent({\r
220 data: null,\r
221 code: Ext.direct.Manager.exceptions.TRANSPORT,\r
222 message: 'Unable to connect to the server.',\r
223 xhr: response\r
224 });\r
225 \r
226 me.fireEvent('data', me, events);\r
227 }\r
228 },\r
229 \r
230 /**\r
231 * @private\r
232 */\r
233 onPollFn: function(result, event, success, options) {\r
234 this.onData(null, success, { responseText: result });\r
235 },\r
236 \r
237 inheritableStatics: {\r
238 /**\r
239 * @private\r
240 * @static\r
241 * @inheritable\r
242 */\r
243 checkConfig: function(config) {\r
244 // Polling provider needs either URI or pollFn\r
245 return config && config.type === 'polling' &&\r
246 (config.url || config.pollFn);\r
247 }\r
248 }\r
249});