|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('io-queue', function(Y) { |
|
9 |
|
10 /** |
|
11 * Extends the IO base class to implement Queue for synchronous |
|
12 * transaction processing. |
|
13 * @module io |
|
14 * @submodule io-queue |
|
15 */ |
|
16 |
|
17 /** |
|
18 * @description Array of transactions queued for processing |
|
19 * |
|
20 * @property _yQ |
|
21 * @private |
|
22 * @static |
|
23 * @type Object |
|
24 */ |
|
25 var _q = new Y.Queue(), |
|
26 |
|
27 /** |
|
28 * @description Reference to "io:complete" event handler. |
|
29 * |
|
30 * @property _e |
|
31 * @private |
|
32 * @static |
|
33 * @type Object |
|
34 */ |
|
35 _e, |
|
36 |
|
37 _activeId, |
|
38 /** |
|
39 * @description Property to determine whether the queue is set to |
|
40 * 1 (active) or 0 (inactive). When inactive, transactions |
|
41 * will be stored in the queue until the queue is set to active. |
|
42 * |
|
43 * @property _qState |
|
44 * @private |
|
45 * @static |
|
46 * @type int |
|
47 */ |
|
48 _qState = 1; |
|
49 |
|
50 /** |
|
51 * @description Method for requesting a transaction, and queueing the |
|
52 * request before it is sent to the resource. |
|
53 * |
|
54 * @method _queue |
|
55 * @private |
|
56 * @static |
|
57 * @return Object |
|
58 */ |
|
59 function _queue(uri, c) { |
|
60 var o = { uri: uri, id: Y.io._id(), cfg:c }; |
|
61 |
|
62 _q.add(o); |
|
63 if (_qState === 1) { |
|
64 _shift(); |
|
65 } |
|
66 |
|
67 return o; |
|
68 } |
|
69 |
|
70 /** |
|
71 * @description Method Process the first transaction from the |
|
72 * queue in FIFO order. |
|
73 * |
|
74 * @method _shift |
|
75 * @private |
|
76 * @static |
|
77 * @return void |
|
78 */ |
|
79 function _shift() { |
|
80 var o = _q.next(); |
|
81 |
|
82 _activeId = o.id; |
|
83 _qState = 0; |
|
84 Y.io(o.uri, o.cfg, o.id); |
|
85 } |
|
86 |
|
87 /** |
|
88 * @description Method for promoting a transaction to the top of the queue. |
|
89 * |
|
90 * @method _unshift |
|
91 * @private |
|
92 * @static |
|
93 * @return void |
|
94 */ |
|
95 function _unshift(o) { |
|
96 _q.promote(o); |
|
97 } |
|
98 |
|
99 function _next(id) { |
|
100 _qState = 1; |
|
101 if (_activeId === id && _q.size() > 0) { |
|
102 _shift(); |
|
103 } |
|
104 } |
|
105 |
|
106 /** |
|
107 * @description Method for removing a specific, pending transaction from |
|
108 * the queue. |
|
109 * |
|
110 * @method _remove |
|
111 * @private |
|
112 * @static |
|
113 * @return void |
|
114 */ |
|
115 function _remove(o) { |
|
116 _q.remove(o); |
|
117 } |
|
118 |
|
119 function _start() { |
|
120 _qState = 1; |
|
121 |
|
122 if (_q.size() > 0) { |
|
123 _shift(); |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * @description Method for setting queue processing to inactive. |
|
129 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
130 * not processed until the queue is reset to "active". |
|
131 * |
|
132 * @method _stop |
|
133 * @private |
|
134 * @static |
|
135 * @return void |
|
136 */ |
|
137 function _stop() { |
|
138 _qState = 0; |
|
139 }; |
|
140 |
|
141 /** |
|
142 * @description Method to query the current size of the queue. |
|
143 * |
|
144 * @method _size |
|
145 * @private |
|
146 * @static |
|
147 * @return int |
|
148 */ |
|
149 function _size() { |
|
150 return _q.size(); |
|
151 }; |
|
152 |
|
153 _e = Y.on('io:complete', function(id) { _next(id); }, Y.io); |
|
154 |
|
155 /** |
|
156 * @description Method to query the current size of the queue, or to |
|
157 * set a maximum queue size. This is the interface for _size(). |
|
158 * |
|
159 * @method size |
|
160 * @public |
|
161 * @static |
|
162 * @param {number} i - Specified maximum size of queue. |
|
163 * @return number |
|
164 */ |
|
165 _queue.size = _size; |
|
166 |
|
167 /** |
|
168 * @description Method for setting the queue to active. If there are |
|
169 * transactions pending in the queue, they will be processed from the |
|
170 * queue in FIFO order. This is the interface for _start(). |
|
171 * |
|
172 * @method start |
|
173 * @public |
|
174 * @static |
|
175 * @return void |
|
176 */ |
|
177 _queue.start = _start; |
|
178 |
|
179 /** |
|
180 * @description Method for setting queue processing to inactive. |
|
181 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
182 * not processed until the queue is restarted. This is the |
|
183 * interface for _stop(). |
|
184 * |
|
185 * @method stop |
|
186 * @public |
|
187 * @static |
|
188 * @return void |
|
189 */ |
|
190 _queue.stop = _stop; |
|
191 |
|
192 /** |
|
193 * @description Method for promoting a transaction to the top of the queue. |
|
194 * This is the interface for _unshift(). |
|
195 * |
|
196 * @method promote |
|
197 * @public |
|
198 * @static |
|
199 * @param {Object} o - Reference to queued transaction. |
|
200 * @return void |
|
201 */ |
|
202 _queue.promote = _unshift; |
|
203 |
|
204 /** |
|
205 * @description Method for removing a specific, pending transaction from |
|
206 * the queue. This is the interface for _purge(). |
|
207 * |
|
208 * @method purge |
|
209 * @public |
|
210 * @static |
|
211 * @param {Object} o - Reference to queued transaction. |
|
212 * @return void |
|
213 */ |
|
214 _queue.remove = _remove; |
|
215 |
|
216 Y.mix(Y.io, { |
|
217 queue: _queue |
|
218 }, true); |
|
219 |
|
220 |
|
221 |
|
222 }, '3.0.0b1' ,{requires:['io-base']}); |