API Docs for: 3.8.0
Show:

File: datasource/js/datasource-polling.js

  1. /**
  2.  * Extends DataSource with polling functionality.
  3.  *
  4.  * @module datasource
  5.  * @submodule datasource-polling
  6.  */
  7.    
  8. /**
  9.  * Adds polling to the DataSource Utility.
  10.  * @class Pollable
  11.  * @extends DataSource.Local
  12.  */    
  13. function Pollable() {
  14.     this._intervals = {};
  15. }

  16. Pollable.prototype = {

  17.     /**
  18.     * @property _intervals
  19.     * @description Hash of polling interval IDs that have been enabled,
  20.     * stored here to be able to clear all intervals.
  21.     * @private
  22.     */
  23.     _intervals: null,

  24.     /**
  25.      * Sets up a polling mechanism to send requests at set intervals and
  26.      * forward responses to given callback.
  27.      *
  28.      * @method setInterval
  29.      * @param msec {Number} Length of interval in milliseconds.
  30.      * @param [request] {Object} An object literal with the following properties:
  31.      *     <dl>
  32.      *     <dt><code>request</code></dt>
  33.      *     <dd>The request to send to the live data source, if any.</dd>
  34.      *     <dt><code>callback</code></dt>
  35.      *     <dd>An object literal with the following properties:
  36.      *         <dl>
  37.      *         <dt><code>success</code></dt>
  38.      *         <dd>The function to call when the data is ready.</dd>
  39.      *         <dt><code>failure</code></dt>
  40.      *         <dd>The function to call upon a response failure condition.</dd>
  41.      *         <dt><code>argument</code></dt>
  42.      *         <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
  43.      *         </dl>
  44.      *     </dd>
  45.      *     <dt><code>cfg</code></dt>
  46.      *     <dd>Configuration object, if any.</dd>
  47.      *     </dl>
  48.      * @return {Number} Interval ID.
  49.      */
  50.     setInterval: function(msec, request) {
  51.         var x = Y.later(msec, this, this.sendRequest, [ request ], true);
  52.         this._intervals[x.id] = x;
  53.         // First call happens immediately, but async
  54.         Y.later(0, this, this.sendRequest, [request]);
  55.         return x.id;
  56.     },

  57.     /**
  58.      * Disables polling mechanism associated with the given interval ID.
  59.      *
  60.      * @method clearInterval
  61.      * @param id {Number} Interval ID.
  62.      */
  63.     clearInterval: function(id, key) {
  64.         // In case of being called by clearAllIntervals()
  65.         id = key || id;
  66.         if(this._intervals[id]) {
  67.             // Clear the interval
  68.             this._intervals[id].cancel();
  69.             // Clear from tracker
  70.             delete this._intervals[id];
  71.         }
  72.     },

  73.     /**
  74.      * Clears all intervals.
  75.      *
  76.      * @method clearAllIntervals
  77.      */
  78.     clearAllIntervals: function() {
  79.         Y.each(this._intervals, this.clearInterval, this);
  80.     }
  81. };
  82.    
  83. Y.augment(Y.DataSource.Local, Pollable);

  84.