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
|
class PromisePriorityQueue {
constructor(concurrent, least) {
this.least = least || 10;
this.concurrent = concurrent || 2;
this.running = 0;
this.queues = [];
for (let i = 0; i < this.least; ++i) {
this.queues[i] = [];
}
this.size = 0;
}
configure(config) {
if (config.least) this.least = config.least;
if (config.concurrent) this.concurrent = config.concurrent;
}
enqueue(i, p, t, ...args) {
return p.bind(t)(...args);
}
// enqueue(i, p, t, ...argv) {
// this.size++;
// return new Promise((res, rej) => {
// this.queues[i-1].push(
// [
// p.bind(t, ...argv),
// res,
// rej
// ]
// );
// this.run();
// });
// };
dequeue() {
for (let i = 0; i < this.least; ++i) {
if (this.queues[i][0] !== undefined) {
this.size--;
return this.queues[i].shift();
}
}
return null;
}
async run() {
if (this.running < this.concurrent) {
let x;
while (x = this.dequeue()) {
await x[0]().then(x[1], x[2]);
}
}
}
}
module.exports = {
queue: new PromisePriorityQueue(),
PromisePriorityQueue: PromisePriorityQueue
};
|