|
525
|
1 |
/* |
|
|
2 |
YUI 3.10.3 (build 2fb5187) |
|
|
3 |
Copyright 2013 Yahoo! Inc. All rights reserved. |
|
|
4 |
Licensed under the BSD License. |
|
|
5 |
http://yuilibrary.com/license/ |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
YUI.add('datasource-polling', function (Y, NAME) { |
|
|
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 |
function Pollable() { |
|
|
23 |
this._intervals = {}; |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
Pollable.prototype = { |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* @property _intervals |
|
|
30 |
* @description Hash of polling interval IDs that have been enabled, |
|
|
31 |
* stored here to be able to clear all intervals. |
|
|
32 |
* @private |
|
|
33 |
*/ |
|
|
34 |
_intervals: null, |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* Sets up a polling mechanism to send requests at set intervals and |
|
|
38 |
* forward responses to given callback. |
|
|
39 |
* |
|
|
40 |
* @method setInterval |
|
|
41 |
* @param msec {Number} Length of interval in milliseconds. |
|
|
42 |
* @param [request] {Object} An object literal with the following properties: |
|
|
43 |
* <dl> |
|
|
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: |
|
|
48 |
* <dl> |
|
|
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> |
|
|
55 |
* </dl> |
|
|
56 |
* </dd> |
|
|
57 |
* <dt><code>cfg</code></dt> |
|
|
58 |
* <dd>Configuration object, if any.</dd> |
|
|
59 |
* </dl> |
|
|
60 |
* @return {Number} Interval ID. |
|
|
61 |
*/ |
|
|
62 |
setInterval: function(msec, request) { |
|
|
63 |
var x = Y.later(msec, this, this.sendRequest, [ request ], true); |
|
|
64 |
this._intervals[x.id] = x; |
|
|
65 |
// First call happens immediately, but async |
|
|
66 |
Y.later(0, this, this.sendRequest, [request]); |
|
|
67 |
return x.id; |
|
|
68 |
}, |
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
* Disables polling mechanism associated with the given interval ID. |
|
|
72 |
* |
|
|
73 |
* @method clearInterval |
|
|
74 |
* @param id {Number} Interval ID. |
|
|
75 |
*/ |
|
|
76 |
clearInterval: function(id, key) { |
|
|
77 |
// In case of being called by clearAllIntervals() |
|
|
78 |
id = key || id; |
|
|
79 |
if(this._intervals[id]) { |
|
|
80 |
// Clear the interval |
|
|
81 |
this._intervals[id].cancel(); |
|
|
82 |
// Clear from tracker |
|
|
83 |
delete this._intervals[id]; |
|
|
84 |
} |
|
|
85 |
}, |
|
|
86 |
|
|
|
87 |
/** |
|
|
88 |
* Clears all intervals. |
|
|
89 |
* |
|
|
90 |
* @method clearAllIntervals |
|
|
91 |
*/ |
|
|
92 |
clearAllIntervals: function() { |
|
|
93 |
Y.each(this._intervals, this.clearInterval, this); |
|
|
94 |
} |
|
|
95 |
}; |
|
|
96 |
|
|
|
97 |
Y.augment(Y.DataSource.Local, Pollable); |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
}, '3.10.3', {"requires": ["datasource-local"]}); |