1 |
efrain |
1 |
YUI.add('datasource-polling', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Extends DataSource with polling functionality.
|
|
|
5 |
*
|
|
|
6 |
* @module datasource
|
|
|
7 |
* @submodule datasource-polling
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Adds polling to the DataSource Utility.
|
|
|
12 |
* @class Pollable
|
|
|
13 |
* @extends DataSource.Local
|
|
|
14 |
*/
|
|
|
15 |
function Pollable() {
|
|
|
16 |
this._intervals = {};
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
Pollable.prototype = {
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* @property _intervals
|
|
|
23 |
* @description Hash of polling interval IDs that have been enabled,
|
|
|
24 |
* stored here to be able to clear all intervals.
|
|
|
25 |
* @private
|
|
|
26 |
*/
|
|
|
27 |
_intervals: null,
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Sets up a polling mechanism to send requests at set intervals and
|
|
|
31 |
* forward responses to given callback.
|
|
|
32 |
*
|
|
|
33 |
* @method setInterval
|
|
|
34 |
* @param msec {Number} Length of interval in milliseconds.
|
|
|
35 |
* @param [request] {Object} An object literal with the following properties:
|
|
|
36 |
* <dl>
|
|
|
37 |
* <dt><code>request</code></dt>
|
|
|
38 |
* <dd>The request to send to the live data source, if any.</dd>
|
|
|
39 |
* <dt><code>callback</code></dt>
|
|
|
40 |
* <dd>An object literal with the following properties:
|
|
|
41 |
* <dl>
|
|
|
42 |
* <dt><code>success</code></dt>
|
|
|
43 |
* <dd>The function to call when the data is ready.</dd>
|
|
|
44 |
* <dt><code>failure</code></dt>
|
|
|
45 |
* <dd>The function to call upon a response failure condition.</dd>
|
|
|
46 |
* <dt><code>argument</code></dt>
|
|
|
47 |
* <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
|
|
|
48 |
* </dl>
|
|
|
49 |
* </dd>
|
|
|
50 |
* <dt><code>cfg</code></dt>
|
|
|
51 |
* <dd>Configuration object, if any.</dd>
|
|
|
52 |
* </dl>
|
|
|
53 |
* @return {Number} Interval ID.
|
|
|
54 |
*/
|
|
|
55 |
setInterval: function(msec, request) {
|
|
|
56 |
var x = Y.later(msec, this, this.sendRequest, [ request ], true);
|
|
|
57 |
this._intervals[x.id] = x;
|
|
|
58 |
// First call happens immediately, but async
|
|
|
59 |
Y.later(0, this, this.sendRequest, [request]);
|
|
|
60 |
return x.id;
|
|
|
61 |
},
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Disables polling mechanism associated with the given interval ID.
|
|
|
65 |
*
|
|
|
66 |
* @method clearInterval
|
|
|
67 |
* @param id {Number} Interval ID.
|
|
|
68 |
*/
|
|
|
69 |
clearInterval: function(id, key) {
|
|
|
70 |
// In case of being called by clearAllIntervals()
|
|
|
71 |
id = key || id;
|
|
|
72 |
if(this._intervals[id]) {
|
|
|
73 |
// Clear the interval
|
|
|
74 |
this._intervals[id].cancel();
|
|
|
75 |
// Clear from tracker
|
|
|
76 |
delete this._intervals[id];
|
|
|
77 |
}
|
|
|
78 |
},
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Clears all intervals.
|
|
|
82 |
*
|
|
|
83 |
* @method clearAllIntervals
|
|
|
84 |
*/
|
|
|
85 |
clearAllIntervals: function() {
|
|
|
86 |
Y.each(this._intervals, this.clearInterval, this);
|
|
|
87 |
}
|
|
|
88 |
};
|
|
|
89 |
|
|
|
90 |
Y.augment(Y.DataSource.Local, Pollable);
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
}, '3.18.1', {"requires": ["datasource-local"]});
|