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