2
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
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
24
Pollable = function() {
28
Pollable.prototype = {
31
* @property _intervals
32
* @description Hash of polling interval IDs that have been enabled,
33
* stored here to be able to clear all intervals.
39
* Sets up a polling mechanism to send requests at set intervals and forward
40
* responses to given callback.
43
* @param msec {Number} Length of interval in milliseconds.
44
* @param request {Object} Request object.
45
* @param callback {Object} An object literal with the following properties:
47
* <dt><code>success</code></dt>
48
* <dd>The function to call when the data is ready.</dd>
49
* <dt><code>failure</code></dt>
50
* <dd>The function to call upon a response failure condition.</dd>
51
* <dt><code>argument</code></dt>
52
* <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
54
* @return {Number} Interval ID.
56
setInterval: function(msec, request, callback) {
57
var x = Y.later(msec, this, this.sendRequest, [request, callback], true);
58
this._intervals[x.id] = x;
63
* Disables polling mechanism associated with the given interval ID.
65
* @method clearInterval
66
* @param id {Number} Interval ID.
68
clearInterval: function(id, key) {
69
// In case of being called by clearAllIntervals()
71
if(this._intervals[id]) {
73
this._intervals[id].cancel();
75
delete this._intervals[id];
80
* Clears all intervals.
82
* @method clearAllIntervals
84
clearAllIntervals: function() {
85
Y.each(this._intervals, this.clearInterval, this);
89
Y.augment(Y.DataSource.Local, Pollable);
93
}, '3.0.0' ,{requires:['datasource-local']});