~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/datasource-polling/datasource-polling.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('datasource-polling', function(Y) {
 
8
 
 
9
/**
 
10
 * Extends DataSource with polling functionality.
 
11
 *
 
12
 * @module datasource
 
13
 * @submodule datasource-polling
 
14
 */
 
15
    
 
16
/**
 
17
 * Adds polling to the DataSource Utility.
 
18
 * @class Pollable
 
19
 * @extends DataSource.Local
 
20
 */    
 
21
function Pollable() {
 
22
    this._intervals = {};
 
23
}
 
24
 
 
25
Pollable.prototype = {
 
26
 
 
27
    /**
 
28
    * @property _intervals
 
29
    * @description Hash of polling interval IDs that have been enabled,
 
30
    * stored here to be able to clear all intervals.
 
31
    * @private
 
32
    */
 
33
    _intervals: null,
 
34
 
 
35
    /**
 
36
     * Sets up a polling mechanism to send requests at set intervals and
 
37
     * forward responses to given callback.
 
38
     *
 
39
     * @method setInterval
 
40
     * @param msec {Number} Length of interval in milliseconds.
 
41
     * @param request {Object} An object literal with the following properties:
 
42
     *     <dl>
 
43
     *     <dt><code>request</code></dt>
 
44
     *     <dd>The request to send to the live data source, if any.</dd>
 
45
     *     <dt><code>callback</code></dt>
 
46
     *     <dd>An object literal with the following properties:
 
47
     *         <dl>
 
48
     *         <dt><code>success</code></dt>
 
49
     *         <dd>The function to call when the data is ready.</dd>
 
50
     *         <dt><code>failure</code></dt>
 
51
     *         <dd>The function to call upon a response failure condition.</dd>
 
52
     *         <dt><code>argument</code></dt>
 
53
     *         <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
 
54
     *         </dl>
 
55
     *     </dd>
 
56
     *     <dt><code>cfg</code></dt>
 
57
     *     <dd>Configuration object, if any.</dd>
 
58
     *     </dl>
 
59
     * @return {Number} Interval ID.
 
60
     */
 
61
    setInterval: function(msec, request) {
 
62
        var x = Y.later(msec, this, this.sendRequest, [ request ], true);
 
63
        this._intervals[x.id] = x;
 
64
        // First call happens immediately, but async
 
65
        Y.later(0, this, this.sendRequest, [request]);
 
66
        return x.id;
 
67
    },
 
68
 
 
69
    /**
 
70
     * Disables polling mechanism associated with the given interval ID.
 
71
     *
 
72
     * @method clearInterval
 
73
     * @param id {Number} Interval ID.
 
74
     */
 
75
    clearInterval: function(id, key) {
 
76
        // In case of being called by clearAllIntervals()
 
77
        id = key || id;
 
78
        if(this._intervals[id]) {
 
79
            // Clear the interval
 
80
            this._intervals[id].cancel();
 
81
            // Clear from tracker
 
82
            delete this._intervals[id];
 
83
        }
 
84
    },
 
85
 
 
86
    /**
 
87
     * Clears all intervals.
 
88
     *
 
89
     * @method clearAllIntervals
 
90
     */
 
91
    clearAllIntervals: function() {
 
92
        Y.each(this._intervals, this.clearInterval, this);
 
93
    }
 
94
};
 
95
    
 
96
Y.augment(Y.DataSource.Local, Pollable);
 
97
 
 
98
 
 
99
}, '3.5.1' ,{requires:['datasource-local']});