datasource-polling.js revision 85d511965757454bd8f7dc3e987518eb06e78d8f
YUI.add('datasource-polling', function(Y) {
/**
* Extends DataSource with polling functionality.
*
* @module datasource-polling
* @requires datasource-base
* @title DataSource Polling Extension
*/
var LANG = Y.Lang,
/**
* Adds polling to the YUI DataSource utility.
* @class Pollable
*/
Pollable = function() {};
Pollable.prototype = {
/**
* @property _intervals
* @description Array of polling interval IDs that have been enabled,
* stored here to be able to clear all intervals.
* @private
*/
_intervals: null,
/**
* Sets up a polling mechanism to send requests at set intervals and forward
* responses to given callback.
*
* @method setInterval
* @param msec {Number} Length of interval in milliseconds.
* @param request {Object} Request object.
* @param callback {Object} An object literal with the following properties:
* <dl>
* <dt><code>success</code></dt>
* <dd>The function to call when the data is ready.</dd>
* <dt><code>failure</code></dt>
* <dd>The function to call upon a response failure condition.</dd>
* <dt><code>scope</code></dt>
* <dd>The object to serve as the scope for the success and failure handlers.</dd>
* <dt><code>argument</code></dt>
* <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
* </dl>
* @return {Number} Interval ID.
*/
setInterval: function(msec, request, callback) {
if(LANG.isNumber(msec) && (msec >= 0)) {
var self = this,
id = setInterval(function() {
self.sendRequest(request, callback);
//self._makeConnection(request, callback);
}, msec);
if(!this._intervals) {
this._intervals = [];
}
this._intervals.push(id);
return id;
}
else {
}
},
/**
* Disables polling mechanism associated with the given interval ID.
*
* @method clearInterval
* @param id {Number} Interval ID.
*/
clearInterval: function(id) {
// Remove from tracker if there
var tracker = this._intervals || [],
i = tracker.length-1;
for(; i>-1; i--) {
if(tracker[i] === id) {
tracker.splice(i,1);
clearInterval(id);
}
}
}
};
//Y.DataSource.Local = Y.Base.build(Y.DataSource.Local.NAME, Y.DataSource.Local, [Pollable]);
}, '@VERSION@' ,{requires:['datasource-base']});