-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-queue.js
More file actions
44 lines (36 loc) · 910 Bytes
/
task-queue.js
File metadata and controls
44 lines (36 loc) · 910 Bytes
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
var TaskQueue = function() {
this._final_callback = null;
this._queue = [];
this.next = function() {
if (this._queue.length == 0) {
if (this._final_callback)
this._final_callback.call(this);
} else {
var next = this._queue.shift();
if (next) next.callback.apply(this, next.arguments);
else this.next();
};
};
this.execute = function(callback) {
this._final_callback = callback;
this.next();
};
this.add = function() {
arguments = Array.prototype.slice.call(arguments, 0);
if (!arguments.length) return;
var task = new Object;
task.callback = arguments.pop();
if (!task.callback) return;
task.arguments = arguments;
this._queue.push(task);
};
this.add_break = function() {
this._queue.push(null);
};
this.break = function() {
while (this._queue.length && !this._queue[0])
this._queue.shift();
this.next();
};
};
exports.TaskQueue = TaskQueue;