forked from cowboy/jquery-message-queuing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.ba-jqmq.js
462 lines (408 loc) · 13.3 KB
/
jquery.ba-jqmq.js
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
/*!
* jQuery Message Queuing - v1.0 - 1/5/2010
* http://benalman.com/projects/jquery-message-queuing-plugin/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Script: jQuery Message Queuing: Get all your JavaScript ducks in a row
//
// *Version: 1.0, Last updated: 1/5/2010*
//
// Project Home - http://benalman.com/projects/jquery-message-queuing-plugin/
// GitHub - http://github.com/cowboy/jquery-message-queuing/
// Source - http://github.com/cowboy/jquery-message-queuing/raw/master/jquery.ba-jqmq.js
// (Minified) - http://github.com/cowboy/jquery-message-queuing/raw/master/jquery.ba-jqmq.min.js (1.2kb)
//
// About: License
//
// Copyright (c) 2010 "Cowboy" Ben Alman,
// Dual licensed under the MIT and GPL licenses.
// http://benalman.com/about/license/
//
// About: Examples
//
// These working examples, complete with fully commented code, illustrate a few
// ways in which this plugin can be used.
//
// Serial AJAX - http://benalman.com/code/projects/jquery-message-queuing/examples/ajax/
// Throttling - http://benalman.com/code/projects/jquery-message-queuing/examples/throttling/
//
// About: Support and Testing
//
// Information about what version or versions of jQuery this plugin has been
// tested with, what browsers it has been tested in, and where the unit tests
// reside (so you can test it yourself).
//
// jQuery Versions - 1.3.2, 1.4a2
// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome, Opera 9.6-10.
// Unit Tests - http://benalman.com/code/projects/jquery-message-queuing/unit/
//
// About: Release History
//
// 1.0 - (1/5/2010) Initial release
(function($,undefined){
'$:nomunge'; // Used by YUI compressor.
// Queue defaults.
var defaults = {
delay: 100,
batch: 1,
/*
callback: null,
complete: null,
paused: false,
*/
queue: []
};
// Method: jQuery.jqmq
//
// Create a new queue.
//
// Usage:
//
// > var queueObj = jQuery.jqmq( 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.
$.jqmq = function( opts ) {
// The queue object to be returned.
var self = {},
// Initialize queue with passed options.
options = $.extend( true, {}, defaults, opts ),
// The actual queue.
queue = options.queue,
paused = options.paused,
recent = [],
timeout_id,
cleared,
// Method references.
addEach,
size,
start;
// Method: queueObj.add
//
// Add a single item onto the queue. If you want to add multiple items onto
// the queue individually, use <queueObj.addEach>. If the queue was empty and
// not paused, processing will resume immediately.
//
// Usage:
//
// > queueObj.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.
self.add = function( item, priority ) {
return addEach( [ item ], priority );
};
// Method: queueObj.addEach
//
// Add multiple items onto the queue, individually. If you want to add a
// single item onto the queue, use <queueObj.add>. If the queue was empty and
// not paused, processing will resume immediately.
//
// Usage:
//
// > queueObj.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.
self.addEach = addEach = function( items, priority ) {
if ( items ) {
// Unset "cleared" status.
cleared = false;
// Push all items, individually, onto the queue. If priority is true, send
// them to the beginning, otherwise, send them to the end.
queue = priority
? items.concat( queue )
: queue.concat( items );
// If queue isn't explicitly paused, restart it.
paused || start();
}
return size();
};
// Method: queueObj.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:
//
// > queueObj.start();
//
// Returns:
//
// Nothing.
self.start = start = function() {
// Flag queue as un-paused.
paused = false;
if ( size() && !timeout_id && !recent.length ) {
(function loopy(){
var delay = options.delay,
batch = options.batch,
complete = options.complete,
callback = options.callback;
// Clear timeout_id.
stop();
// If queue is empty, call the "complete" method if it exists and quit.
if ( !size() ) {
cleared = true;
complete && complete.call( self );
return;
}
// Queue has items, so shift off the first `batch` items.
recent = queue.splice( 0, batch );
// If "callback" method returns true, unshift the queue items for
// another attempt.
if ( callback && callback.call( self, batch === 1 ? recent[0] : recent ) === true ) {
queue = recent.concat( queue );
recent = [];
}
// Repeatedly loop if the delay is a number >= 0, otherwise wait for a
// $.jqmqNext() call.
if ( typeof delay === 'number' && delay >= 0 ) {
recent = [];
timeout_id = setTimeout( loopy, delay );
}
})();
}
};
// Method: queueObj.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:
//
// > queueObj.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.
self.next = function( retry ) {
var complete = options.complete;
// If retry is true, unshift the most recent items for another attempt.
if ( retry ) {
queue = recent.concat( queue );
}
// Reset the recent items list.
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 ( size() ) {
paused || start();
} else if ( !cleared ) {
cleared = true;
complete && complete.call( self );
}
};
// Method: queueObj.clear
//
// Clear a queue completely. The paused/started status of the queue is
// unchanged.
//
// Usage:
//
// > queueObj.clear();
//
// Returns:
//
// (Array) The previous queue contents.
self.clear = function() {
var result = queue;
// Stop the queue if it is running.
stop();
// Clear the queue.
queue = [];
cleared = true;
// Reset the recent items list.
recent = [];
// Return the previous queue, in case it's needed for some reason.
return result;
};
// Method: queueObj.pause
//
// Pause a currently running queue. A paused but empty queue will need to be
// manually restarted with <queueObj.start> even after new items are added.
//
// Usage:
//
// > queueObj.pause();
//
// Returns:
//
// Nothing.
self.pause = function() {
// Stop the queue if it is running.
stop();
// Flag it as paused.
paused = true;
};
// Method: queueObj.update
//
// Update an existing queue's options.
//
// Usage:
//
// > queueObj.update( options );
//
// Arguments:
//
// options - (Object) An object containing options specific to this queue.
//
// Options:
//
// The delay, batch, callback and complete options from <jQuery.jqmq> can
// be updated. The queue and paused state can be changed using the other
// queueObj methods.
//
// Returns:
//
// Nothing.
self.update = function( opts ) {
$.extend( options, opts );
};
// Method: queueObj.size
//
// Get the current queue length.
//
// Usage:
//
// > queueObj.size();
//
// Returns:
//
// (Number) The length of the queue.
self.size = size = function() {
return queue.length;
};
// Method: queueObj.indexOf
//
// Get the current index in the queue of the passed item.
//
// Usage:
//
// > queueObj.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.
self.indexOf = function( item ) {
return $.inArray( item, queue );
};
// Stop a running queue, optionally flagging it as paused.
function stop() {
timeout_id && clearTimeout( timeout_id );
timeout_id = undefined;
};
// If queue isn't explicitly paused, start it.
paused || start();
return self;
};
// Method: jQuery.fn.jqmqAdd
//
// Add a jQuery collection of elements onto the queue. If you want to add each
// selected element onto the queue individually, use <jQuery.fn.jqmqAddEach>. If
// the queue was empty and not paused, processing will resume immediately.
//
// Usage:
//
// > jQuery('selector').jqmqAdd( queueObj [, priority ] );
//
// Arguments:
//
// queueObj - (Object) A valid jqmq object, returned from <jQuery.jqmq>.
// 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:
//
// (jQuery) The initial jQuery collection of elements.
$.fn.jqmqAdd = function( queueObj, priority ) {
queueObj.add( this.get(), priority );
return this;
};
// Method: jQuery.fn.jqmqAddEach
//
// Add each selected element from a jQuery collection onto the queue,
// individually. If you want to add the entire jQuery collection of elements
// onto the queue as a single item, use <jQuery.fn.jqmqAdd>. If the queue was
// empty and not paused, processing will resume immediately.
//
// Usage:
//
// > jQuery('selector').jqmqAddEach( queueObj [, priority ] );
//
// Arguments:
//
// queueObj - (Object) A valid jqmq object, returned from <jQuery.jqmq>.
// 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:
//
// (jQuery) The initial jQuery collection of elements.
$.fn.jqmqAddEach = function( queueObj, priority ) {
queueObj.addEach( this.get(), priority );
return this;
};
})(jQuery);