|
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 return o; |
|
84 }, |
|
85 |
|
86 _qNext: function(id) { |
|
87 var io = this; |
|
88 io._qState = 1; |
|
89 if (io._qActiveId === id && io._q.size() > 0) { |
|
90 io._qShift(); |
|
91 } |
|
92 }, |
|
93 |
|
94 /** |
|
95 * Method for promoting a transaction to the top of the queue. |
|
96 * |
|
97 * @method promote |
|
98 * @static |
|
99 */ |
|
100 qPromote: function(o) { |
|
101 this._q.promote(o); |
|
102 }, |
|
103 |
|
104 /** |
|
105 * Method for removing a specific, pending transaction from |
|
106 * the queue. |
|
107 * |
|
108 * @method remove |
|
109 * @private |
|
110 * @static |
|
111 */ |
|
112 qRemove: function(o) { |
|
113 this._q.remove(o); |
|
114 }, |
|
115 |
|
116 /** |
|
117 * Method for cancel all pending transaction from |
|
118 * the queue. |
|
119 * |
|
120 * @method empty |
|
121 * @static |
|
122 * @since 3.7.3 |
|
123 */ |
|
124 qEmpty: function() { |
|
125 this._q = new Y.Queue(); |
|
126 }, |
|
127 |
|
128 qStart: function() { |
|
129 var io = this; |
|
130 io._qState = 1; |
|
131 |
|
132 if (io._q.size() > 0) { |
|
133 io._qShift(); |
|
134 } |
|
135 }, |
|
136 |
|
137 /** |
|
138 * Method for setting queue processing to inactive. |
|
139 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
140 * not processed until the queue is reset to "active". |
|
141 * |
|
142 * @method _stop |
|
143 * @private |
|
144 * @static |
|
145 */ |
|
146 qStop: function() { |
|
147 this._qState = 0; |
|
148 }, |
|
149 |
|
150 /** |
|
151 * Method to query the current size of the queue. |
|
152 * |
|
153 * @method _size |
|
154 * @private |
|
155 * @static |
|
156 * @return {Number} |
|
157 */ |
|
158 qSize: function() { |
|
159 return this._q.size(); |
|
160 } |
|
161 |
|
162 }, true); |
|
163 |
|
164 function _queue(u, c) { |
|
165 return io.queue.apply(io, [u, c]); |
|
166 } |
|
167 |
|
168 _queue.start = function () { io.qStart(); }; |
|
169 _queue.stop = function () { io.qStop(); }; |
|
170 _queue.promote = function (o) { io.qPromote(o); }; |
|
171 _queue.remove = function (o) { io.qRemove(o); }; |
|
172 _queue.size = function () { io.qSize(); }; |
|
173 _queue.empty = function () { io.qEmpty(); }; |
|
174 Y.io.queue = _queue; |
|
175 |
|
176 |
|
177 }, '3.10.3', {"requires": ["io-base", "queue-promote"]}); |