2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('datasource-polling', function(Y) {
11 * Extends DataSource with polling functionality.
14 * @submodule datasource-polling
18 * Adds polling to the DataSource Utility.
20 * @extends DataSource.Local
26 Pollable.prototype = {
29 * @property _intervals
30 * @description Hash of polling interval IDs that have been enabled,
31 * stored here to be able to clear all intervals.
37 * Sets up a polling mechanism to send requests at set intervals and
38 * forward responses to given callback.
41 * @param msec {Number} Length of interval in milliseconds.
42 * @param request {Object} An object literal with the following properties:
44 * <dt><code>request</code></dt>
45 * <dd>The request to send to the live data source, if any.</dd>
46 * <dt><code>callback</code></dt>
47 * <dd>An object literal with the following properties:
49 * <dt><code>success</code></dt>
50 * <dd>The function to call when the data is ready.</dd>
51 * <dt><code>failure</code></dt>
52 * <dd>The function to call upon a response failure condition.</dd>
53 * <dt><code>argument</code></dt>
54 * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
57 * <dt><code>cfg</code></dt>
58 * <dd>Configuration object, if any.</dd>
60 * @return {Number} Interval ID.
62 setInterval: function(msec, callback) {
63 var x = Y.later(msec, this, this.sendRequest, [ callback ], true);
64 this._intervals[x.id] = x;
69 * Disables polling mechanism associated with the given interval ID.
71 * @method clearInterval
72 * @param id {Number} Interval ID.
74 clearInterval: function(id, key) {
75 // In case of being called by clearAllIntervals()
77 if(this._intervals[id]) {
79 this._intervals[id].cancel();
81 delete this._intervals[id];
86 * Clears all intervals.
88 * @method clearAllIntervals
90 clearAllIntervals: function() {
91 Y.each(this._intervals, this.clearInterval, this);
95 Y.augment(Y.DataSource.Local, Pollable);
99 }, '3.3.0' ,{requires:['datasource-local']});