-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (77 loc) · 2.29 KB
/
index.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
var Enqueuer = require('./lib/Enqueuer');
var Schedulable = require('./lib/Schedulable');
module.exports = function() {
var queues = {};
var cleaning = false;
var that = this;
this.createQueue = function(name, complete) {
queues[name] = new Enqueuer(complete);
};
this.cancelAll = function(){
cleaning = true;
const promises = [];
for (var property in queues) {
if (queues.hasOwnProperty(property)) {
promises.push( queues[property].cancelPromise() );
}
}
Promise.all(promises).then(()=>{
cleaning = false;
});
};
this.hasTasks = function(name) {
return queues[name].hasTasks();
};
function hasNewTasks(name) {
if (!queues[name].isRunning() && queues[name].hasTasks()) {
return true;
}
return false;
}
this.enqueuePriority = function(name, func, args) {
if(cleaning){
return;
}
that.enqueue(name, func, args, true);
};
this.enqueue = function(name, func, args, priority) {
if(cleaning){
return;
}
var schedulable;
if (queues[name] !== undefined) {
var self = null;
if (args.self) {
self = args.self;
}
if (!args.callback) {
args.callback = function() {};
}
schedulable = new Schedulable(func, args, self);
if (priority) {
queues[name].enqueuePriority(schedulable);
} else {
queues[name].enqueue(schedulable);
}
if (hasNewTasks(name)) {
that.trigger(name);
}
} else {
throw { err: "The queue named " + name + " doesn't exist!" };
}
};
this.trigger = function(name) {
if (queues[name] !== undefined) {
queues[name].start();
} else {
throw { err: "The queue named " + name + " doesn't exist!" };
}
};
this.cancel = function(name) {
if (queues[name] !== undefined) {
queues[name].cancel();
} else {
throw { err: "The queue named " + name + " doesn't exist!" };
}
};
};