-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskQueue.js
106 lines (90 loc) · 2.3 KB
/
TaskQueue.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
/*
* A TaskQueue class based on callBack Style or continuation-passing style (CPS)
* */
class TaskQueue {
constructor(concurrency) {
this.concurrency = concurrency;
this.running = 0;
this.queue = [];
}
pushTask(task) {
this.queue.push(task);
this.next();
}
next() {
while (this.running < this.concurrency && this.queue.length) {
const task = this.queue.shift();
task(() => {
this.running--;
this.next();
});
this.running++;
}
}
};
/*
* A TaskQueue class based on Promise Style
* */
class TaskQueue {
constructor(concurrency) {
this.concurrency = concurrency;
this.running = 0;
this.queue = [];
}
pushTask(task) {
this.queue.push(task);
this.next();
}
next() {
while (this.running < this.concurrency && this.queue.length) {
const task = this.queue.shift();
task().then(() => {
this.running--;
this.next();
});
this.running++;
}
}
};
/*
* A taskQueue based on generator which implement producer-consumer pattern.
* To understand this class, you need know generator\co\producer-consumer, etc.
* */
"use strict";
const co = require('co');
class TaskQueue {
constructor(concurrency) {
this.concurrency = concurrency;
this.running = 0;
this.taskQueue = [];
this.consumerQueue = [];
this.spawnWorkers(concurrency);
}
pushTask(task) {
if (this.consumerQueue.length !== 0) {
this.consumerQueue.shift()(null, task);
} else {
this.taskQueue.push(task);
}
}
spawnWorkers(concurrency) {
const self = this;
for(let i = 0; i < concurrency; i++) {
co(function* () {
while(true) {
const task = yield self.nextTask();
yield task;
}
});
}
}
nextTask() {
return callback => {
if(this.taskQueue.length !== 0) {
return callback(null, this.taskQueue.shift());
}
this.consumerQueue.push(callback);
}
}
}
module.exports = TaskQueue;