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