|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('datasource-polling', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Extends DataSource with polling functionality. |
|
|
12 |
* |
|
|
13 |
* @module datasource |
|
|
14 |
* @submodule datasource-polling |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Adds polling to the DataSource Utility. |
|
|
19 |
* @class Pollable |
|
|
20 |
* @extends DataSource.Local |
|
|
21 |
*/ |
|
|
22 |
var LANG = Y.Lang, |
|
|
23 |
|
|
|
24 |
Pollable = function() { |
|
|
25 |
this._intervals = {}; |
|
|
26 |
}; |
|
|
27 |
|
|
|
28 |
Pollable.prototype = { |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* @property _intervals |
|
|
32 |
* @description Hash of polling interval IDs that have been enabled, |
|
|
33 |
* stored here to be able to clear all intervals. |
|
|
34 |
* @private |
|
|
35 |
*/ |
|
|
36 |
_intervals: null, |
|
|
37 |
|
|
|
38 |
/** |
|
|
39 |
* Sets up a polling mechanism to send requests at set intervals and forward |
|
|
40 |
* responses to given callback. |
|
|
41 |
* |
|
|
42 |
* @method setInterval |
|
|
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: |
|
|
46 |
* <dl> |
|
|
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> |
|
|
53 |
* </dl> |
|
|
54 |
* @return {Number} Interval ID. |
|
|
55 |
*/ |
|
|
56 |
setInterval: function(msec, request, callback) { |
|
|
57 |
var x = Y.later(msec, this, this.sendRequest, [request, callback], true); |
|
|
58 |
this._intervals[x.id] = x; |
|
|
59 |
return x.id; |
|
|
60 |
}, |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Disables polling mechanism associated with the given interval ID. |
|
|
64 |
* |
|
|
65 |
* @method clearInterval |
|
|
66 |
* @param id {Number} Interval ID. |
|
|
67 |
*/ |
|
|
68 |
clearInterval: function(id, key) { |
|
|
69 |
// In case of being called by clearAllIntervals() |
|
|
70 |
id = key || id; |
|
|
71 |
if(this._intervals[id]) { |
|
|
72 |
// Clear the interval |
|
|
73 |
this._intervals[id].cancel(); |
|
|
74 |
// Clear from tracker |
|
|
75 |
delete this._intervals[id]; |
|
|
76 |
} |
|
|
77 |
}, |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Clears all intervals. |
|
|
81 |
* |
|
|
82 |
* @method clearAllIntervals |
|
|
83 |
*/ |
|
|
84 |
clearAllIntervals: function() { |
|
|
85 |
Y.each(this._intervals, this.clearInterval, this); |
|
|
86 |
} |
|
|
87 |
}; |
|
|
88 |
|
|
|
89 |
Y.augment(Y.DataSource.Local, Pollable); |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
}, '3.0.0b1' ,{requires:['datasource-local']}); |