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