-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path8-pipeline.js
90 lines (72 loc) · 2.04 KB
/
8-pipeline.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
'use strict';
const { Readable, Writable, Transform, pipeline } = 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) {
const task = this.waiting.shift();
this.count++;
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 fs = require('node:fs');
const fileWStream = fs.createWriteStream('./tasks.txt');
const stringifyStream = new Transform({
readableObjectMode: true,
writableObjectMode: true,
write(data, encoding, next) {
const result = JSON.stringify(data);
this.push(result + '\n');
next();
},
});
const writable = new Writable({
objectMode: true,
write(data, encoding, next) {
console.log({ data });
next();
},
});
const job = (task, next) => {
setTimeout(next, task.interval, null, task);
};
const queue = QueueStream.channels(3).process(job);
pipeline(queue, stringifyStream, fileWStream, (error) => {
if (error) throw error;
console.log('pipeline done');
});
queue.pipe(writable);
// queue.on('data', (data) => void console.log(data));
queue.on('end', () => void console.log('tasks end'));
queue.on('close', () => void console.log('stream closed'));
writable.on('finish', () => void console.log('writable finished'));
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 });
}