3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('datasource-polling', function(Y) {
10
* Extends DataSource with polling functionality.
13
* @submodule datasource-polling
17
* Adds polling to the DataSource Utility.
19
* @extends DataSource.Local
25
Pollable.prototype = {
28
* @property _intervals
29
* @description Hash of polling interval IDs that have been enabled,
30
* stored here to be able to clear all intervals.
36
* Sets up a polling mechanism to send requests at set intervals and
37
* forward responses to given callback.
40
* @param msec {Number} Length of interval in milliseconds.
41
* @param request {Object} An object literal with the following properties:
43
* <dt><code>request</code></dt>
44
* <dd>The request to send to the live data source, if any.</dd>
45
* <dt><code>callback</code></dt>
46
* <dd>An object literal with the following properties:
48
* <dt><code>success</code></dt>
49
* <dd>The function to call when the data is ready.</dd>
50
* <dt><code>failure</code></dt>
51
* <dd>The function to call upon a response failure condition.</dd>
52
* <dt><code>argument</code></dt>
53
* <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
56
* <dt><code>cfg</code></dt>
57
* <dd>Configuration object, if any.</dd>
59
* @return {Number} Interval ID.
61
setInterval: function(msec, request) {
62
var x = Y.later(msec, this, this.sendRequest, [ request ], true);
63
this._intervals[x.id] = x;
64
// First call happens immediately, but async
65
Y.later(0, this, this.sendRequest, [request]);
70
* Disables polling mechanism associated with the given interval ID.
72
* @method clearInterval
73
* @param id {Number} Interval ID.
75
clearInterval: function(id, key) {
76
// In case of being called by clearAllIntervals()
78
if(this._intervals[id]) {
80
this._intervals[id].cancel();
82
delete this._intervals[id];
87
* Clears all intervals.
89
* @method clearAllIntervals
91
clearAllIntervals: function() {
92
Y.each(this._intervals, this.clearInterval, this);
96
Y.augment(Y.DataSource.Local, Pollable);
99
}, '3.5.1' ,{requires:['datasource-local']});