1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('async-queue', function (Y, NAME) {
5
* <p>AsyncQueue allows you create a chain of function callbacks executed
6
* via setTimeout (or synchronously) that are guaranteed to run in order.
7
* Items in the queue can be promoted or removed. Start or resume the
8
* execution chain with run(). pause() to temporarily delay execution, or
9
* stop() to halt and clear the queue.</p>
15
* <p>A specialized queue class that supports scheduling callbacks to execute
16
* sequentially, iteratively, even asynchronously.</p>
18
* <p>Callbacks can be function refs or objects with the following keys. Only
19
* the <code>fn</code> key is required.</p>
22
* <li><code>fn</code> -- The callback function</li>
23
* <li><code>context</code> -- The execution context for the callbackFn.</li>
24
* <li><code>args</code> -- Arguments to pass to callbackFn.</li>
25
* <li><code>timeout</code> -- Millisecond delay before executing callbackFn.
26
* (Applies to each iterative execution of callback)</li>
27
* <li><code>iterations</code> -- Number of times to repeat the callback.
28
* <li><code>until</code> -- Repeat the callback until this function returns
29
* true. This setting trumps iterations.</li>
30
* <li><code>autoContinue</code> -- Set to false to prevent the AsyncQueue from
31
* executing the next callback in the Queue after
32
* the callback completes.</li>
33
* <li><code>id</code> -- Name that can be used to get, promote, get the
34
* indexOf, or delete this callback.</li>
38
* @extends EventTarget
40
* @param callback* {Function|Object} 0..n callbacks to seed the queue
42
Y.AsyncQueue = function() {
44
this.add.apply(this, arguments);
47
var Queue = Y.AsyncQueue,
53
isObject = Y.Lang.isObject,
54
isFunction = Y.Lang.isFunction;
57
* <p>Static default values used to populate callback configuration properties.
58
* Preconfigured defaults include:</p>
61
* <li><code>autoContinue</code>: <code>true</code></li>
62
* <li><code>iterations</code>: 1</li>
63
* <li><code>timeout</code>: 10 (10ms between callbacks)</li>
64
* <li><code>until</code>: (function to run until iterations <= 0)</li>
71
Queue.defaults = Y.mix({
77
return this.iterations <= 0;
79
}, Y.config.queueDefaults || {});
81
Y.extend(Queue, Y.EventTarget, {
83
* Used to indicate the queue is currently executing a callback.
86
* @type {Boolean|Object} true for synchronous callback execution, the
87
* return handle from Y.later for async callbacks.
94
* Initializes the AsyncQueue instance properties and events.
100
Y.EventTarget.call(this, { prefix: 'queue', emitFacade: true });
105
* Callback defaults for this instance. Static defaults that are not
106
* overridden are also included.
117
* Initializes the instance events.
119
* @method _initEvents
122
_initEvents : function () {
124
'execute' : { defaultFn : this._defExecFn, emitFacade: true },
125
'shift' : { defaultFn : this._defShiftFn, emitFacade: true },
126
'add' : { defaultFn : this._defAddFn, emitFacade: true },
127
'promote' : { defaultFn : this._defPromoteFn, emitFacade: true },
128
'remove' : { defaultFn : this._defRemoveFn, emitFacade: true }
133
* Returns the next callback needing execution. If a callback is
134
* configured to repeat via iterations or until, it will be returned until
135
* the completion criteria is met.
137
* When the queue is empty, null is returned.
140
* @return {Function} the callback to execute
145
while (this._q.length) {
146
callback = this._q[0] = this._prepare(this._q[0]);
147
if (callback && callback.until()) {
148
this.fire(SHIFT, { callback: callback });
155
return callback || null;
159
* Default functionality for the "shift" event. Shifts the
160
* callback stored in the event object's <em>callback</em> property from
161
* the queue if it is the first item.
163
* @method _defShiftFn
164
* @param e {Event} The event object
167
_defShiftFn : function (e) {
168
if (this.indexOf(e.callback) === 0) {
174
* Creates a wrapper function to execute the callback using the aggregated
175
* configuration generated by combining the static AsyncQueue.defaults, the
176
* instance defaults, and the specified callback settings.
178
* The wrapper function is decorated with the callback configuration as
179
* properties for runtime modification.
182
* @param callback {Object|Function} the raw callback
183
* @return {Function} a decorated function wrapper to execute the callback
186
_prepare: function (callback) {
187
if (isFunction(callback) && callback._prepared) {
191
var config = Y.merge(
193
{ context : this, args: [], _prepared: true },
195
(isFunction(callback) ? { fn: callback } : callback)),
197
wrapper = Y.bind(function () {
198
if (!wrapper._running) {
199
wrapper.iterations--;
201
if (isFunction(wrapper.fn)) {
202
wrapper.fn.apply(wrapper.context || Y,
203
Y.Array(wrapper.args));
207
return Y.mix(wrapper, config);
211
* Sets the queue in motion. All queued callbacks will be executed in
212
* order unless pause() or stop() is called or if one of the callbacks is
213
* configured with autoContinue: false.
216
* @return {AsyncQueue} the AsyncQueue instance
223
for (callback = this.next();
224
cont && callback && !this.isRunning();
225
callback = this.next())
227
cont = (callback.timeout < 0) ?
228
this._execute(callback) :
229
this._schedule(callback);
234
* Event fired after the last queued callback is executed.
237
this.fire('complete');
244
* Handles the execution of callbacks. Returns a boolean indicating
245
* whether it is appropriate to continue running.
248
* @param callback {Object} the callback object to execute
249
* @return {Boolean} whether the run loop should continue
252
_execute : function (callback) {
253
this._running = callback._running = true;
255
callback.iterations--;
256
this.fire(EXECUTE, { callback: callback });
258
var cont = this._running && callback.autoContinue;
260
this._running = callback._running = false;
266
* Schedules the execution of asynchronous callbacks.
269
* @param callback {Object} the callback object to execute
270
* @return {Boolean} whether the run loop should continue
273
_schedule : function (callback) {
274
this._running = Y.later(callback.timeout, this, function () {
275
if (this._execute(callback)) {
284
* Determines if the queue is waiting for a callback to complete execution.
287
* @return {Boolean} true if queue is waiting for a
288
* from any initiated transactions
290
isRunning : function () {
291
return !!this._running;
295
* Default functionality for the "execute" event. Executes the
299
* @param e {Event} the event object
302
_defExecFn : function (e) {
307
* Add any number of callbacks to the end of the queue. Callbacks may be
308
* provided as functions or objects.
311
* @param callback* {Function|Object} 0..n callbacks
312
* @return {AsyncQueue} the AsyncQueue instance
316
this.fire('add', { callbacks: Y.Array(arguments,0,true) });
322
* Default functionality for the "add" event. Adds the callbacks
323
* in the event facade to the queue. Callbacks successfully added to the
324
* queue are present in the event's <code>added</code> property in the
328
* @param e {Event} the event object
331
_defAddFn : function(e) {
335
Y.Array.each(e.callbacks, function (c) {
346
* Pause the execution of the queue after the execution of the current
347
* callback completes. If called from code outside of a queued callback,
348
* clears the timeout for the pending callback. Paused queue can be
349
* restarted with q.run()
352
* @return {AsyncQueue} the AsyncQueue instance
356
if (isObject(this._running)) {
357
this._running.cancel();
360
this._running = false;
366
* Stop and clear the queue after the current execution of the
367
* current callback completes.
370
* @return {AsyncQueue} the AsyncQueue instance
380
* Returns the current index of a callback. Pass in either the id or
381
* callback function from getCallback.
384
* @param callback {String|Function} the callback or its specified id
385
* @return {Number} index of the callback or -1 if not found
387
indexOf : function (callback) {
388
var i = 0, len = this._q.length, c;
390
for (; i < len; ++i) {
392
if (c === callback || c.id === callback) {
401
* Retrieve a callback by its id. Useful to modify the configuration
402
* while the queue is running.
404
* @method getCallback
405
* @param id {String} the id assigned to the callback
406
* @return {Object} the callback object
408
getCallback : function (id) {
409
var i = this.indexOf(id);
411
return (i > -1) ? this._q[i] : null;
415
* Promotes the named callback to the top of the queue. If a callback is
416
* currently executing or looping (via until or iterations), the promotion
417
* is scheduled to occur after the current callback has completed.
420
* @param callback {String|Object} the callback object or a callback's id
421
* @return {AsyncQueue} the AsyncQueue instance
424
promote : function (callback) {
425
var payload = { callback : callback },e;
427
if (this.isRunning()) {
428
e = this.after(SHIFT, function () {
429
this.fire(PROMOTE, payload);
433
this.fire(PROMOTE, payload);
440
* <p>Default functionality for the "promote" event. Promotes the
441
* named callback to the head of the queue.</p>
443
* <p>The event object will contain a property "callback", which
444
* holds the id of a callback or the callback object itself.</p>
446
* @method _defPromoteFn
447
* @param e {Event} the custom event
450
_defPromoteFn : function (e) {
451
var i = this.indexOf(e.callback),
452
promoted = (i > -1) ? this._q.splice(i,1)[0] : null;
454
e.promoted = promoted;
457
this._q.unshift(promoted);
462
* Removes the callback from the queue. If the queue is active, the
463
* removal is scheduled to occur after the current callback has completed.
466
* @param callback {String|Object} the callback object or a callback's id
467
* @return {AsyncQueue} the AsyncQueue instance
470
remove : function (callback) {
471
var payload = { callback : callback },e;
473
// Can't return the removed callback because of the deferral until
474
// current callback is complete
475
if (this.isRunning()) {
476
e = this.after(SHIFT, function () {
477
this.fire(REMOVE, payload);
481
this.fire(REMOVE, payload);
488
* <p>Default functionality for the "remove" event. Removes the
489
* callback from the queue.</p>
491
* <p>The event object will contain a property "callback", which
492
* holds the id of a callback or the callback object itself.</p>
494
* @method _defRemoveFn
495
* @param e {Event} the custom event
498
_defRemoveFn : function (e) {
499
var i = this.indexOf(e.callback);
501
e.removed = (i > -1) ? this._q.splice(i,1)[0] : null;
505
* Returns the number of callbacks in the queue.
511
// next() flushes callbacks that have met their until() criteria and
512
// therefore shouldn't count since they wouldn't execute anyway.
513
if (!this.isRunning()) {
517
return this._q.length;
523
}, '3.9.1', {"requires": ["event-custom"]});