2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('queue', function(Y) {
10
* Mechanism to execute a series of callbacks in a non-blocking queue. Each
11
* callback is executed via setTimout unless configured with a negative
12
* timeout, in which case it is run in blocking mode in the same execution
13
* thread as the previous callback. Callbacks can be function references or
14
* object literals with the following keys:
16
* <li><code>fn</code> - {Function} REQUIRED the callback function.</li>
17
* <li><code>timeout</code> - {number} millisecond delay to wait after previous callback completion before executing this callback. Negative values cause immediate blocking execution. Default 0.</li>
18
* <li><code>until</code> - {Function} boolean function executed before each iteration. Return true to indicate callback completion.</li>
19
* <li><code>iterations</code> - {Number} number of times to execute the callback before proceeding to the next callback in the queue. Incompatible with <code>until</code>.</li>
25
* @param callback* {Function|Object} Any number of callbacks to initialize the queue
27
Y.Queue = function () {
28
// Factory or Constructor
29
var me = this instanceof Y.Queue ? this : new Y.Queue();
39
return me.add.apply(me,arguments);
44
* Timeout id used to pause or stop execution and indicate the execution
45
* state of the Queue. 0 indicates paused or stopped, negatives indicate
46
* blocking execution, and positives indicate non-blocking execution.
54
* Execute the queue callbacks (also resumes paused Queue).
56
* @return {Queue} the Queue instance
59
// Grab the first callback in the queue
63
// If there is no callback in the queue or the Queue is currently
64
// in an execution mode, return
67
* Event fired after the last queued callback is executed. Not
68
* fired if the Queue is stopped via q.stop().
79
if (typeof fn === 'function') {
80
var ms = c.timeout || 0,
83
// Execute immediately if the callback timeout is negative.
86
if (c.until) { // test .until condition
90
} else if (c.iterations) { // test .iterations
91
for (;c.iterations-- > 0;) {
94
} else { // single shot callback
101
if (c.until) { // test .until condition
103
// Move to the next callback
107
} else if (!c.iterations || !--c.iterations) { // .iterations
111
// Set to execute after the configured timeout
112
this.id = setTimeout(function () {
115
// Loop unless the Queue was paused from inside the callback
117
// Indicate ready to run state
119
// Start the fun all over again
130
* Executes the callback function
132
* @param fn {Function} the function to execute
133
* @param c {Object|Function} the callback as defined during add(c)
136
_exec : function (fn,c) {
138
* Fired before a callback is executed
139
* @event beforeCallback
140
* @param o {Object} Object literal with the following keys:
142
* <dt>fn</dt><dd>The function about to be executed</dd>
143
* <dt>callback</dt><dd>The callback as provided to <code>add(..)</code></dd>
146
this.fire('beforeCallback',{fn:fn,callback:c});
151
* Fired before a callback is executed
152
* @event afterCallback
153
* @param o {Object} Object literal with the following keys:
155
* <dt>fn</dt><dd>The function just executed</dd>
156
* <dt>callback</dt><dd>The callback as provided to <code>add(..)</code></dd>
159
this.fire('afterCallback',{fn:fn,callback:c});
163
* Shifts the first callback off the Queue
167
_shift : function () {
169
* Fired after a callback is shifted from the Queue
170
* @event shiftCallback
171
* @param callback {Function|Object} The callback passed to <code>add(..)</code>
173
this.fire('shiftCallback',this.q.shift());
177
* Add any number of callbacks to the end of the queue
179
* @param callback* {Function|Object} Any number of callbacks
180
* @return {Queue} the Queue instance
183
var callbacks = Y.Array(arguments,0,true);
184
this.q.splice.apply(this.q,[this.q.length,0].concat(callbacks));
187
* Fired from within <code>add(..)</code> after callbacks are queued
189
* @param callbacks {Array} Array of callbacks passed to <code>add(..)</code>
191
this.fire('addCallback',callbacks);
196
* Pause the execution of the Queue after the execution of the current
197
* callback completes. If called from code outside of a queued callback,
198
* clears the timeout for the pending callback. Paused Queue can be
199
* restarted with q.run()
201
* @return {Queue} the Queue instance
204
clearTimeout(this.id);
208
* Fired after Queue is paused
216
* Stop and clear the Queue's queue after the current execution of the
217
* current callback completes.
219
* @return {Queue} the Queue instance
226
* Fired after Queue is stopped
233
Y.augment(Y.Queue,Y.Event.Target);
236
}, '3.0.0pr2' ,{requires:['event']});