forked from frozeman/meteor-build-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
55 lines (46 loc) · 1.04 KB
/
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
50
51
52
53
54
55
// FIFO
module.exports = function() {
var self = this;
var invokations = [];
var paused = true;
var maxLength = 0;
self.progress = function(count, total) {
// console.log(count + ' of ' + total);
};
self.reset = function() {
paused = true;
invokations = [];
};
self.add = function(f) {
if (paused) {
if (typeof f !== 'function') {
throw new Error('queue requires function');
}
invokations.push(f);
}
};
self.next = function(text) {
if (text) {
self.reset();
console.log(' ' + text.red);
}
if (!paused) {
if (invokations.length) {
var f = invokations.shift();
// Update the progress
self.progress(invokations.length, maxLength);
setTimeout(function() {
// Run function
f(self.next);
}, 0);
}
}
};
self.run = function() {
paused = false;
maxLength = invokations.length;
// Update the progress
self.progress(invokations.length, maxLength);
self.next();
};
};