~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.13.0/datasource-polling/datasource-polling-debug.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.13.0 (build 508226d)
 
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.13.0', {"requires": ["datasource-local"]});