Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / io-queue / io-queue.js
blob3d4e63acbe39945d71e5a6dc40e47a311218979c
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('io-queue', function(Y) {
9 /**
10 Extends IO to implement Queue for synchronous
11 transaction processing.
12 @module io
13 @submodule io-queue
14 @for IO
15 **/
16 var io = Y.io._map['io:0'] || new Y.IO();
18 Y.mix(Y.IO.prototype, {
19    /**
20     * Array of transactions queued for processing
21     *
22     * @property _q
23     * @private
24     * @static
25     * @type {Object}
26     */
27     _q: new Y.Queue(),
28     _qActiveId: null,
29     _qInit: false,
30    /**
31     * Property to determine whether the queue is set to
32     * 1 (active) or 0 (inactive).  When inactive, transactions
33     * will be stored in the queue until the queue is set to active.
34     *
35     * @property _qState
36     * @private
37     * @static
38     * @type {Number}
39     */
40     _qState: 1,
42    /**
43     * Method Process the first transaction from the
44     * queue in FIFO order.
45     *
46     * @method _qShift
47     * @private
48     * @static
49     */
50     _qShift: function() {
51         var io = this,
52             o = io._q.next();
54         io._qActiveId = o.id;
55         io._qState = 0;
56         io.send(o.uri, o.cfg, o.id);
57     },
59    /**
60     * Method for queueing a transaction before the request is sent to the
61     * resource, to ensure sequential processing.
62     *
63     * @method queue
64     * @static
65     * @return {Object}
66     */
67     queue: function(uri, c) {
68         var io = this,
69             o = { uri: uri, cfg:c, id: this._id++ };
71         if(!io._qInit) {
72             Y.on('io:complete', function(id, o) { io._qNext(id); }, io);
73             io._qInit = true;
74         }
76         io._q.add(o);
77         if (io._qState === 1) {
78             io._qShift();
79         }
81         return o;
82     },
84     _qNext: function(id) {
85         var io = this;
86         io._qState = 1;
87         if (io._qActiveId === id && io._q.size() > 0) {
88             io._qShift();
89         }
90     },
92    /**
93     * Method for promoting a transaction to the top of the queue.
94     *
95     * @method promote
96     * @static
97     */
98     qPromote: function(o) {
99         this._q.promote(o);
100     },
102    /**
103     * Method for removing a specific, pending transaction from
104     * the queue.
105     *
106     * @method remove
107     * @private
108     * @static
109     */
110     qRemove: function(o) {
111         this._q.remove(o);
112     },
114     qStart: function() {
115         var io = this;
116         io._qState = 1;
118         if (io._q.size() > 0) {
119             io._qShift();
120         }
121     },
123    /**
124     * Method for setting queue processing to inactive.
125     * Transaction requests to YUI.io.queue() will be stored in the queue, but
126     * not processed until the queue is reset to "active".
127     *
128     * @method _stop
129     * @private
130     * @static
131     */
132     qStop: function() {
133         this._qState = 0;
134     },
136    /**
137     * Method to query the current size of the queue.
138     *
139     * @method _size
140     * @private
141     * @static
142     * @return {Number}
143     */
144     qSize: function() {
145         return this._q.size();
146     }
148 }, true);
150 function _queue(u, c) {
151     return io.queue.apply(io, [u, c]);
154 _queue.start = function () { io.qStart(); };
155 _queue.stop = function () { io.qStop(); };
156 _queue.promote = function (o) { io.qPromote(o); };
157 _queue.remove = function (o) { io.qRemove(o); };
158 _queue.size = function () { io.qSize(); };
159 Y.io.queue = _queue;
162 }, '3.5.0' ,{requires:['io-base','queue-promote']});