|
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 Y.log('Object queued. Transaction id is' + o.id, 'info', 'io'); |
|
68 return o; |
|
69 } |
|
70 |
|
71 /** |
|
72 * @description Method Process the first transaction from the |
|
73 * queue in FIFO order. |
|
74 * |
|
75 * @method _shift |
|
76 * @private |
|
77 * @static |
|
78 * @return void |
|
79 */ |
|
80 function _shift() { |
|
81 var o = _q.next(); |
|
82 |
|
83 _activeId = o.id; |
|
84 _qState = 0; |
|
85 Y.io(o.uri, o.cfg, o.id); |
|
86 } |
|
87 |
|
88 /** |
|
89 * @description Method for promoting a transaction to the top of the queue. |
|
90 * |
|
91 * @method _unshift |
|
92 * @private |
|
93 * @static |
|
94 * @return void |
|
95 */ |
|
96 function _unshift(o) { |
|
97 _q.promote(o); |
|
98 } |
|
99 |
|
100 function _next(id) { |
|
101 _qState = 1; |
|
102 if (_activeId === id && _q.size() > 0) { |
|
103 _shift(); |
|
104 } |
|
105 } |
|
106 |
|
107 /** |
|
108 * @description Method for removing a specific, pending transaction from |
|
109 * the queue. |
|
110 * |
|
111 * @method _remove |
|
112 * @private |
|
113 * @static |
|
114 * @return void |
|
115 */ |
|
116 function _remove(o) { |
|
117 _q.remove(o); |
|
118 } |
|
119 |
|
120 function _start() { |
|
121 _qState = 1; |
|
122 |
|
123 if (_q.size() > 0) { |
|
124 _shift(); |
|
125 } |
|
126 Y.log('Queue started.', 'info', 'io'); |
|
127 } |
|
128 |
|
129 /** |
|
130 * @description Method for setting queue processing to inactive. |
|
131 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
132 * not processed until the queue is reset to "active". |
|
133 * |
|
134 * @method _stop |
|
135 * @private |
|
136 * @static |
|
137 * @return void |
|
138 */ |
|
139 function _stop() { |
|
140 _qState = 0; |
|
141 Y.log('Queue stopped.', 'info', 'io'); |
|
142 }; |
|
143 |
|
144 /** |
|
145 * @description Method to query the current size of the queue. |
|
146 * |
|
147 * @method _size |
|
148 * @private |
|
149 * @static |
|
150 * @return int |
|
151 */ |
|
152 function _size() { |
|
153 return _q.size(); |
|
154 }; |
|
155 |
|
156 _e = Y.on('io:complete', function(id) { _next(id); }, Y.io); |
|
157 |
|
158 /** |
|
159 * @description Method to query the current size of the queue, or to |
|
160 * set a maximum queue size. This is the interface for _size(). |
|
161 * |
|
162 * @method size |
|
163 * @public |
|
164 * @static |
|
165 * @param {number} i - Specified maximum size of queue. |
|
166 * @return number |
|
167 */ |
|
168 _queue.size = _size; |
|
169 |
|
170 /** |
|
171 * @description Method for setting the queue to active. If there are |
|
172 * transactions pending in the queue, they will be processed from the |
|
173 * queue in FIFO order. This is the interface for _start(). |
|
174 * |
|
175 * @method start |
|
176 * @public |
|
177 * @static |
|
178 * @return void |
|
179 */ |
|
180 _queue.start = _start; |
|
181 |
|
182 /** |
|
183 * @description Method for setting queue processing to inactive. |
|
184 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
185 * not processed until the queue is restarted. This is the |
|
186 * interface for _stop(). |
|
187 * |
|
188 * @method stop |
|
189 * @public |
|
190 * @static |
|
191 * @return void |
|
192 */ |
|
193 _queue.stop = _stop; |
|
194 |
|
195 /** |
|
196 * @description Method for promoting a transaction to the top of the queue. |
|
197 * This is the interface for _unshift(). |
|
198 * |
|
199 * @method promote |
|
200 * @public |
|
201 * @static |
|
202 * @param {Object} o - Reference to queued transaction. |
|
203 * @return void |
|
204 */ |
|
205 _queue.promote = _unshift; |
|
206 |
|
207 /** |
|
208 * @description Method for removing a specific, pending transaction from |
|
209 * the queue. This is the interface for _purge(). |
|
210 * |
|
211 * @method purge |
|
212 * @public |
|
213 * @static |
|
214 * @param {Object} o - Reference to queued transaction. |
|
215 * @return void |
|
216 */ |
|
217 _queue.remove = _remove; |
|
218 |
|
219 Y.mix(Y.io, { |
|
220 queue: _queue |
|
221 }, true); |
|
222 |
|
223 |
|
224 |
|
225 }, '3.0.0b1' ,{requires:['io-base']}); |