-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
sink.js
44 lines (35 loc) · 963 Bytes
/
sink.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
'use strict';
var Writable = require('readable-stream/writable');
function listenerCount(stream, evt) {
return stream.listeners(evt).length;
}
function sink(stream) {
var sinkStream = new Writable({
objectMode: true,
write: function(file, enc, cb) {
cb();
},
});
stream.on('removeListener', function() {
if (listenerCount(stream, 'readable') || listenerCount(stream, 'data')) {
stream.unpipe(sinkStream);
}
});
stream.on('unpipe', function() {
if (!(listenerCount(stream, 'readable') || listenerCount(stream, 'data'))) {
stream.pipe(sinkStream);
}
});
stream.on('newListener', function() {
if (listenerCount(stream, 'readable') || listenerCount(stream, 'data')) {
stream.unpipe(sinkStream);
}
});
return function() {
if (listenerCount(stream, 'readable') || listenerCount(stream, 'data')) {
return;
}
stream.pipe(sinkStream);
};
}
module.exports = sink;