-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
50 lines (41 loc) · 975 Bytes
/
queue.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
/******
* Tiny Queue
*
* @constructor
*/
clippy.Queue = function (onEmptyCallback) {
this._queue = [];
this._onEmptyCallback = onEmptyCallback;
};
clippy.Queue.prototype = {
/***
*
* @param {function(Function)} func
* @returns {jQuery.Deferred}
*/
queue:function (func) {
this._queue.push(func);
if (this._queue.length === 1 && !this._active) {
this._progressQueue();
}
},
_progressQueue:function () {
// stop if nothing left in queue
if (!this._queue.length) {
this._onEmptyCallback();
return;
}
var f = this._queue.shift();
this._active = true;
// execute function
var completeFunction = $.proxy(this.next, this);
f(completeFunction);
},
clear:function () {
this._queue = [];
},
next:function () {
this._active = false;
this._progressQueue();
}
};