3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('async-queue', function(Y) {
10 * <p>AsyncQueue allows you create a chain of function callbacks executed
11 * via setTimeout (or synchronously) that are guaranteed to run in order.
12 * Items in the queue can be promoted or removed. Start or resume the
13 * execution chain with run(). pause() to temporarily delay execution, or
14 * stop() to halt and clear the queue.</p>
20 * <p>A specialized queue class that supports scheduling callbacks to execute
21 * sequentially, iteratively, even asynchronously.</p>
23 * <p>Callbacks can be function refs or objects with the following keys. Only
24 * the <code>fn</code> key is required.</p>
27 * <li><code>fn</code> -- The callback function</li>
28 * <li><code>context</code> -- The execution context for the callbackFn.</li>
29 * <li><code>args</code> -- Arguments to pass to callbackFn.</li>
30 * <li><code>timeout</code> -- Millisecond delay before executing callbackFn.
31 * (Applies to each iterative execution of callback)</li>
32 * <li><code>iterations</code> -- Number of times to repeat the callback.
33 * <li><code>until</code> -- Repeat the callback until this function returns
34 * true. This setting trumps iterations.</li>
35 * <li><code>autoContinue</code> -- Set to false to prevent the AsyncQueue from
36 * executing the next callback in the Queue after
37 * the callback completes.</li>
38 * <li><code>id</code> -- Name that can be used to get, promote, get the
39 * indexOf, or delete this callback.</li>
43 * @extends EventTarget
45 * @param callback* {Function|Object} 0..n callbacks to seed the queue
47 Y.AsyncQueue = function() {
49 this.add.apply(this, arguments);
52 var Queue = Y.AsyncQueue,
58 isObject = Y.Lang.isObject,
59 isFunction = Y.Lang.isFunction;
62 * <p>Static default values used to populate callback configuration properties.
63 * Preconfigured defaults include:</p>
66 * <li><code>autoContinue</code>: <code>true</code></li>
67 * <li><code>iterations</code>: 1</li>
68 * <li><code>timeout</code>: 10 (10ms between callbacks)</li>
69 * <li><code>until</code>: (function to run until iterations <= 0)</li>
76 Queue.defaults = Y.mix({
82 return this.iterations <= 0;
84 }, Y.config.queueDefaults || {});
86 Y.extend(Queue, Y.EventTarget, {
88 * Used to indicate the queue is currently executing a callback.
91 * @type {Boolean|Object} true for synchronous callback execution, the
92 * return handle from Y.later for async callbacks.
99 * Initializes the AsyncQueue instance properties and events.
104 _init : function () {
105 Y.EventTarget.call(this, { prefix: 'queue', emitFacade: true });
110 * Callback defaults for this instance. Static defaults that are not
111 * overridden are also included.
122 * Initializes the instance events.
124 * @method _initEvents
127 _initEvents : function () {
129 'execute' : { defaultFn : this._defExecFn, emitFacade: true },
130 'shift' : { defaultFn : this._defShiftFn, emitFacade: true },
131 'add' : { defaultFn : this._defAddFn, emitFacade: true },
132 'promote' : { defaultFn : this._defPromoteFn, emitFacade: true },
133 'remove' : { defaultFn : this._defRemoveFn, emitFacade: true }
138 * Returns the next callback needing execution. If a callback is
139 * configured to repeat via iterations or until, it will be returned until
140 * the completion criteria is met.
142 * When the queue is empty, null is returned.
145 * @return {Function} the callback to execute
150 while (this._q.length) {
151 callback = this._q[0] = this._prepare(this._q[0]);
152 if (callback && callback.until()) {
153 this.fire(SHIFT, { callback: callback });
160 return callback || null;
164 * Default functionality for the "shift" event. Shifts the
165 * callback stored in the event object's <em>callback</em> property from
166 * the queue if it is the first item.
168 * @method _defShiftFn
169 * @param e {Event} The event object
172 _defShiftFn : function (e) {
173 if (this.indexOf(e.callback) === 0) {
179 * Creates a wrapper function to execute the callback using the aggregated
180 * configuration generated by combining the static AsyncQueue.defaults, the
181 * instance defaults, and the specified callback settings.
183 * The wrapper function is decorated with the callback configuration as
184 * properties for runtime modification.
187 * @param callback {Object|Function} the raw callback
188 * @return {Function} a decorated function wrapper to execute the callback
191 _prepare: function (callback) {
192 if (isFunction(callback) && callback._prepared) {
196 var config = Y.merge(
198 { context : this, args: [], _prepared: true },
200 (isFunction(callback) ? { fn: callback } : callback)),
202 wrapper = Y.bind(function () {
203 if (!wrapper._running) {
204 wrapper.iterations--;
206 if (isFunction(wrapper.fn)) {
207 wrapper.fn.apply(wrapper.context || Y,
208 Y.Array(wrapper.args));
212 return Y.mix(wrapper, config);
216 * Sets the queue in motion. All queued callbacks will be executed in
217 * order unless pause() or stop() is called or if one of the callbacks is
218 * configured with autoContinue: false.
221 * @return {AsyncQueue} the AsyncQueue instance
228 for (callback = this.next();
229 cont && callback && !this.isRunning();
230 callback = this.next())
232 cont = (callback.timeout < 0) ?
233 this._execute(callback) :
234 this._schedule(callback);
239 * Event fired after the last queued callback is executed.
242 this.fire('complete');
249 * Handles the execution of callbacks. Returns a boolean indicating
250 * whether it is appropriate to continue running.
253 * @param callback {Object} the callback object to execute
254 * @return {Boolean} whether the run loop should continue
257 _execute : function (callback) {
258 this._running = callback._running = true;
260 callback.iterations--;
261 this.fire(EXECUTE, { callback: callback });
263 var cont = this._running && callback.autoContinue;
265 this._running = callback._running = false;
271 * Schedules the execution of asynchronous callbacks.
274 * @param callback {Object} the callback object to execute
275 * @return {Boolean} whether the run loop should continue
278 _schedule : function (callback) {
279 this._running = Y.later(callback.timeout, this, function () {
280 if (this._execute(callback)) {
289 * Determines if the queue is waiting for a callback to complete execution.
292 * @return {Boolean} true if queue is waiting for a
293 * from any initiated transactions
295 isRunning : function () {
296 return !!this._running;
300 * Default functionality for the "execute" event. Executes the
304 * @param e {Event} the event object
307 _defExecFn : function (e) {
312 * Add any number of callbacks to the end of the queue. Callbacks may be
313 * provided as functions or objects.
316 * @param callback* {Function|Object} 0..n callbacks
317 * @return {AsyncQueue} the AsyncQueue instance
321 this.fire('add', { callbacks: Y.Array(arguments,0,true) });
327 * Default functionality for the "add" event. Adds the callbacks
328 * in the event facade to the queue. Callbacks successfully added to the
329 * queue are present in the event's <code>added</code> property in the
333 * @param e {Event} the event object
336 _defAddFn : function(e) {
340 Y.Array.each(e.callbacks, function (c) {
351 * Pause the execution of the queue after the execution of the current
352 * callback completes. If called from code outside of a queued callback,
353 * clears the timeout for the pending callback. Paused queue can be
354 * restarted with q.run()
357 * @return {AsyncQueue} the AsyncQueue instance
361 if (isObject(this._running)) {
362 this._running.cancel();
365 this._running = false;
371 * Stop and clear the queue after the current execution of the
372 * current callback completes.
375 * @return {AsyncQueue} the AsyncQueue instance
385 * Returns the current index of a callback. Pass in either the id or
386 * callback function from getCallback.
389 * @param callback {String|Function} the callback or its specified id
390 * @return {Number} index of the callback or -1 if not found
392 indexOf : function (callback) {
393 var i = 0, len = this._q.length, c;
395 for (; i < len; ++i) {
397 if (c === callback || c.id === callback) {
406 * Retrieve a callback by its id. Useful to modify the configuration
407 * while the queue is running.
409 * @method getCallback
410 * @param id {String} the id assigned to the callback
411 * @return {Object} the callback object
413 getCallback : function (id) {
414 var i = this.indexOf(id);
416 return (i > -1) ? this._q[i] : null;
420 * Promotes the named callback to the top of the queue. If a callback is
421 * currently executing or looping (via until or iterations), the promotion
422 * is scheduled to occur after the current callback has completed.
425 * @param callback {String|Object} the callback object or a callback's id
426 * @return {AsyncQueue} the AsyncQueue instance
429 promote : function (callback) {
430 var payload = { callback : callback },e;
432 if (this.isRunning()) {
433 e = this.after(SHIFT, function () {
434 this.fire(PROMOTE, payload);
438 this.fire(PROMOTE, payload);
445 * <p>Default functionality for the "promote" event. Promotes the
446 * named callback to the head of the queue.</p>
448 * <p>The event object will contain a property "callback", which
449 * holds the id of a callback or the callback object itself.</p>
451 * @method _defPromoteFn
452 * @param e {Event} the custom event
455 _defPromoteFn : function (e) {
456 var i = this.indexOf(e.callback),
457 promoted = (i > -1) ? this._q.splice(i,1)[0] : null;
459 e.promoted = promoted;
462 this._q.unshift(promoted);
467 * Removes the callback from the queue. If the queue is active, the
468 * removal is scheduled to occur after the current callback has completed.
471 * @param callback {String|Object} the callback object or a callback's id
472 * @return {AsyncQueue} the AsyncQueue instance
475 remove : function (callback) {
476 var payload = { callback : callback },e;
478 // Can't return the removed callback because of the deferral until
479 // current callback is complete
480 if (this.isRunning()) {
481 e = this.after(SHIFT, function () {
482 this.fire(REMOVE, payload);
486 this.fire(REMOVE, payload);
493 * <p>Default functionality for the "remove" event. Removes the
494 * callback from the queue.</p>
496 * <p>The event object will contain a property "callback", which
497 * holds the id of a callback or the callback object itself.</p>
499 * @method _defRemoveFn
500 * @param e {Event} the custom event
503 _defRemoveFn : function (e) {
504 var i = this.indexOf(e.callback);
506 e.removed = (i > -1) ? this._q.splice(i,1)[0] : null;
510 * Returns the number of callbacks in the queue.
516 // next() flushes callbacks that have met their until() criteria and
517 // therefore shouldn't count since they wouldn't execute anyway.
518 if (!this.isRunning()) {
522 return this._q.length;
528 }, '3.5.0' ,{requires:['event-custom']});