-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (41 loc) · 1.06 KB
/
index.js
File metadata and controls
51 lines (41 loc) · 1.06 KB
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
const Duplex = require('stream').Duplex;
class EmitterStream extends Duplex {
constructor(highWaterMark = 128) {
super({
objectMode: true,
highWaterMark: highWaterMark
});
}
_read(size) {
}
_write(chunk, encoding, callback) {
this.emit(chunk.name, ...chunk.args);
callback();
}
static input(emitter, ...eventNames) {
let eventStream = new EmitterStream();
let handlersToCleanup = [];
eventNames.forEach(eventName => {
let handler = (...args) => {
eventStream.push({
name: eventName,
args: args
});
};
handlersToCleanup.push(handler);
emitter.on(eventName, handler);
});
eventStream.on('finish', () => {
eventNames.forEach((eventName, i) => {
emitter.removeListener(eventName, handlersToCleanup[i]);
});
});
return eventStream;
}
static output(otherStream) {
let eventStream = new EmitterStream();
otherStream.pipe(eventStream);
return eventStream;
}
};
module.exports = EmitterStream;