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