-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path7-stream.js
60 lines (49 loc) · 1.42 KB
/
7-stream.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
'use strict';
const { Readable } = require('node:stream');
class QueueStream extends Readable {
constructor(concurrency) {
super({ objectMode: true });
this.concurrency = concurrency;
this.count = 0;
this.waiting = [];
}
static channels(concurrency) {
return new QueueStream(concurrency);
}
add(task) {
this.waiting.push(task);
}
_read() {
const emptyChannels = this.concurrency - this.count;
let launchCount = Math.min(emptyChannels, this.waiting.length);
while (launchCount-- > 0) {
this.count++;
const task = this.waiting.shift();
this.onProcess(task, (error, res) => {
if (error) this.emit('error', error);
this.push({ error, res });
this.count--;
});
}
if (this.waiting.length === 0 && this.count === 0) {
this.push(null);
}
}
process(listener) {
this.onProcess = listener;
return this;
}
}
// Usage
const job = (task, next) => {
setTimeout(next, task.interval, null, task);
};
const queue = QueueStream.channels(3).process(job);
for (let i = 0; i < 20; i++) {
if (i < 10) queue.add({ name: `Task${i}`, interval: 1000 });
else queue.add({ name: `Task${i}`, interval: i * 100 });
}
queue.on('data', (data) => void console.log(data));
queue.on('end', () => void console.log('Tasks end'));
queue.on('close', () => void console.log('Stream closed'));
queue.on('error', (error) => void console.error(error));