-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueManager.js
More file actions
516 lines (486 loc) · 30.3 KB
/
QueueManager.js
File metadata and controls
516 lines (486 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*!
* Queuing Manager - v1.0 - 14/08/2013
* http://maggiben.github.io/QueueManager
*
* Copyright (c) 2013 Benjamin Maggi
* Dual licensed under the MIT and GPL licenses.
*/
////////////////////////////////////////////////////////////////////////////////
// *Version: 1.0, Last updated: 14/08/2013* //
// //
// GitHub - http://maggiben.github.io/QueueManager //
// Source - http://github.com/maggiben/QueueManager.git //
// //
// About: Acknowledgements //
// //
// Most of this code is based on "jQuery Message Queuing" plugin by //
// "Cowboy" Ben Alman //
// Website: http://benalman.com/projects/jquery-message-queuing-plugin/ //
// GitHub: http://github.com/cowboy/jquery-message-queuing/ //
// //
// About: License //
// //
// Copyright (c) 2013 Benjamin Maggi, //
// Dual licensed under the MIT and GPL licenses. //
// //
// About: Release History //
// //
// 1.0.0 - (14/08/2013) Initial release //
// 1.0.1 - (15/08/2013) Remove jQuery dependency //
// //
// About: Usage //
// //
// Create a new queue. //
// //
// > var queue = new QueueManager( options ); //
// //
// Arguments: //
// //
// options - (Object) An object containing options specific to this queue. //
// //
// Options: //
// //
// delay - (Number) Time in milliseconds between each callback execution. //
// If delay is -1, queue will wait for a <queueObj.next> call instead of //
// auto-executing. Defaults to 100. //
// batch - (Number) Number of queue items to process at a time. If less //
// than this number of items remain in the queue, the remainder will be //
// processed. Defaults to 1. //
// queue - (Array) Populate the queue initially with these items. Defaults //
// to an empty initial queue. //
// callback - (Function) Called for each queue item or batch of items, //
// every delay milliseconds. This function is passed a single argument, //
// which is the single queue item if batch is 1, or an array of queue //
// items if batch is > 1. If callback returns true, the queue item(s) //
// will be re-added back onto the front of the queue for the next //
// callback execution to retry. Inside this function, `this` refers to //
// the queueObj object. //
// complete - (Function) Called whenever there are no longer any queue //
// items to process. After completion, if more queue items are added //
// and the queue completes again, this function will be called again. //
// Inside this function, `this` refers to the queueObj object. //
// paused - (Boolean) If true, initialize this queue in a paused state. //
// Defaults to false. //
// //
// Returns: //
// //
// (Object) a reference to the jqmq queue object. //
////////////////////////////////////////////////////////////////////////////////
(function (root, factory) {
'use strict';
if (typeof exports === 'object') {
////////////////////////////////////////////////////////////////////////
// Node. Does not work with strict CommonJS, but //
// only CommonJS-like enviroments that support module.exports, //
// like Node. //
////////////////////////////////////////////////////////////////////////
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals (root is window)
root.QueueManager = factory();
}
}(this, function () {
'use strict';
////////////////////////////////////////////////////////////////////////////
// Extend an object with another object's properties. //
////////////////////////////////////////////////////////////////////////////
function extend(object) {
// Takes an unlimited number of extenders.
var args = Array.prototype.slice.call(arguments, 1);
// For each extender, copy their properties on our object.
for (var i = 0, source; source = args[i]; i++) {
if (!source) continue;
for (var property in source) {
object[property] = source[property];
}
}
return object;
}
var QueueManager = (function() {
////////////////////////////////////////////////////////////////////////
// Constructor //
////////////////////////////////////////////////////////////////////////
function QueueManager(settings) {
this.version = '1.0';
var defaults = {
delay: 100,
batch: 1,
callback: null,
complete: null,
paused: false,
queue: []
};
// Merge options
this.options = extend({}, defaults, settings);
// The actual queue.
this.queue = this.options.queue;
this.paused = this.options.paused;
this.recent = [];
this.timeoutId = null;
this.cleared = false;
// If queue isn't explicitly paused, start it.
if(!this.paused) {
this.start();
}
}
////////////////////////////////////////////////////////////////////////
// Method: QueuManager.each //
// //
// Usage: //
// //
// > QueueManager.each( function(item) {} ); //
// //
// Arguments: //
// //
// args - (Function) Called for each queue item or batch of items, //
// every delay milliseconds. This function is passed a single //
// argument, which is the single queue item if batch is 1, or an //
// array of queue items if batch is > 1. If callback returns //
// true, the queue item(s) will be re-added back onto the front //
// of the queue for the next callback execution, to retry. //
// Inside this function, `this` refers to the QueueManager //
// object. //
// //
// Returns: //
// //
// (Object) Reference to QueueManager object. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.each = function(args) {
var that = this;
this.onEach = args;
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.complete //
// //
// Processes to be executed after queue completion. //
// //
// Usage: //
// //
// > QueueManager.complete( function() {} ); //
// //
// Arguments: //
// //
// args - (Function) Called whenever there are no longer any queue //
// items to process. After completion, if more queue items are //
// added and the queue completes again, this function will be //
// called again. Inside this function, `this` refers to the //
// queueObj object. //
// //
// Returns: //
// //
// (Object) Reference to QueueManager object. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.complete = function(args) {
var that = this;
that.onComplete = args;
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.add //
// //
// Add a single item onto the queue. If you want to add multiple //
// items onto the queue individually, use <QueueManager.addEach>. //
// If the queue was empty and not paused, processing will resume //
// immediately. //
// //
// Usage: //
// //
// > QueueManager.add( item [, priority ] ); //
// //
// Arguments: //
// //
// item - (Anything) A single item to add to the queue. //
// priority - (Boolean) If true, the item is added to the front of //
// the queue, otherwise the item is added to the end of the queue. //
// Defaults to false. //
// //
// Returns: //
// //
// (Number) The length of the queue, after the item has been added. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.add = function( item, priority ) {
var that = this;
return that.addEach( [ item ], priority );
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.addEach //
// //
// Add multiple items onto the queue, individually. If you want to //
// add a single item onto the queue, use <QueueManager.add>. If the //
// queue was empty and not paused, processing will resume //
// immediately. //
// //
// Usage: //
// //
// > QueueManager.addEach( items [, priority ] ); //
// //
// Arguments: //
// //
// items - (Array) An array of items to add to the queue. //
// priority - (Boolean) If true, the items are added to the front //
// of the queue, otherwise the items are added to the end of the //
// queue. Defaults to false. //
// //
// Returns: //
// //
// (Number) The length of the queue, after the items have been added.//
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.addEach = function( items, priority ) {
var that = this;
if ( items ) {
// Unset "cleared" status.
that.cleared = false;
// Push all items, individually, onto the queue. If priority is true, send
// them to the beginning, otherwise, send them to the end.
that.queue = priority ? items.concat( that.queue ) : that.queue.concat( items );
// If queue isn't explicitly paused, restart it.
if(!that.paused) {
that.start();
}
}
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.start //
// //
// Start a currently paused queue. If an empty queue is started, //
// it will automatically start processing items as soon as they //
// are added. //
// //
// Usage: //
// //
// > QueueManager.start(); //
// //
// Returns: //
// //
// Nothing. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.start = function() {
// Flag queue as un-paused.
var that = this;
this.paused = false;
if ( this.size() && !this.timeoutId && !this.recent.length ) {
(function loopy(){
var delay = that.options.delay;
var batch = that.options.batch;
var complete = that.options.complete || that.onComplete;
var callback = that.options.callback || that.onEach;
// Clear timeoutId.
that.stop();
// If queue is empty, call the "complete" method if it exists and quit.
if ( !that.size() ) {
that.cleared = true;
if(complete) {
complete.apply( that );
}
return that;
}
// Queue has items, so shift off the first `batch` items.
that.recent = that.queue.splice( 0, batch );
// If "callback" method returns true, unshift the queue items for
// another attempt.
if ( callback && callback.apply( that, [(batch === 1 ? that.recent[0] : that.recent)] ) === true ) {
that.queue = that.recent.concat( that.queue );
that.recent = [];
}
// Repeatedly loop if the delay is a number >= 0, otherwise wait for a
// $.jqmqNext() call.
if ( typeof delay === 'number' && delay >= 0 ) {
that.recent = [];
that.timeoutId = setTimeout( loopy, delay );
}
})();
}
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.next //
// //
// Intended to be called from within the <jQuery.jqmq> callback, //
// this method will continue a queue with a delay of -1. This is most //
// useful for queues of asynchronous-but-serial actions, like AJAX //
// requests that must execute in order, but not overlap. //
// //
// Usage: //
// //
// > QueueManager.next( [ retry ] ); //
// //
// Arguments: //
// //
// retry - (Boolean) If true, the queue item(s) will be re-added //
// back to the front of the queue to be retried on the next queue //
// execution. //
// //
// Returns: //
// //
// Nothing. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.next = function( retry ) {
var that = this;
var complete = that.options.complete || that.onComplete;
// If retry is true, unshift the most recent items for another attempt.
if ( retry ) {
that.queue = that.recent.concat( that.queue );
}
// Reset the recent items list.
that.recent = [];
// If queue is empty (but not from calling .clear), call the "complete"
// method if it exists, otherwise continue processing the queue (if not
// paused).
if ( that.size() ) {
that.start();
} else if ( !that.cleared ) {
that.cleared = true;
if(complete) {
complete.apply( that );
}
}
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.clear //
// //
// Clear a queue completely. The paused/started status of the queue //
// is unchanged. //
// //
// Usage: //
// //
// > QueueManager.clear(); //
// //
// Returns: //
// //
// (Array) The previous queue contents. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.clear = function() {
var that = this;
var result = that.queue;
// Stop the queue if it is running.
that.stop();
// Clear the queue.
that.queue = [];
that.cleared = true;
// Reset the recent items list.
that.recent = [];
// Return the previous queue, in case it's needed for some reason.
return result;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.pause //
// //
// Pause a currently running queue. A paused but empty queue will //
// need to be manually restarted with <QueueManager.start> even after //
// new items are added. //
// //
// Usage: //
// //
// > QueueManager.pause(); //
// //
// Returns: //
// //
// Nothing. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.pause = function() {
var that = this;
// Stop the queue if it is running.
that.stop();
// Flag it as paused.
that.paused = true;
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.update //
// //
// Update an existing queue's options. //
// //
// Usage: //
// //
// > QueueManager.update( options ); //
// //
// Arguments: //
// //
// options - (Object) An object containing options specific to //
// this queue. //
// //
// Options: //
// //
// The delay, batch, callback and complete options from //
// <QueueManager> can be updated. The queue and paused state can be //
// changed using the other QueueManager methods. //
// //
// Returns: //
// //
// Nothing. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.update = function( opts ) {
var that = this;
extend( that.options, opts );
return that;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.size //
// //
// Get the current queue length. //
// //
// Usage: //
// //
// > QueueManager.size(); //
// //
// Returns: //
// //
// (Number) The length of the queue. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.size = function() {
var that = this;
return that.queue.length;
};
////////////////////////////////////////////////////////////////////////
// Method: QueueManager.indexOf //
// //
// Get the current index in the queue of the passed item. //
// //
// Usage: //
// //
// > QueueManager.indexOf( item ); //
// //
// Arguments: //
// //
// item - (Anything) An item to test the index of. //
// //
// Returns: //
// //
// (Number) The index of the passed item in the queue. Returns -1 //
// if not found. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.indexOf = function( item ) {
var that = this;
return that.queue.indexOf(item);
};
////////////////////////////////////////////////////////////////////////
// Stop a running queue, optionally flagging it as paused. //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.stop = function() {
var that = this;
if(that.timeoutId) {
clearTimeout(that.timeoutId);
that.timeoutId = undefined;
}
return that;
};
////////////////////////////////////////////////////////////////////////
// Get module version number //
////////////////////////////////////////////////////////////////////////
QueueManager.prototype.getVersion = function() {
return this.version;
};
return QueueManager;
}());
////////////////////////////////////////////////////////////////////////////
// Just return a value to define the module export. //
// This example returns an object, but the module //
// can return a function as the exported value. //
////////////////////////////////////////////////////////////////////////////
return QueueManager;
}));