Skip to content

Commit 1b02a46

Browse files
committed
more updates, nextTick the removable of listeners in tests
1 parent 3a0cf5b commit 1b02a46

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/sink.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,43 @@ function hasListeners(stream) {
1111
}
1212

1313
function sink(stream) {
14+
var sinkAdded = false;
1415
var sinkStream = new Writable({
1516
objectMode: true,
1617
write: function(file, enc, cb) {
1718
cb();
1819
},
1920
});
2021

22+
function addSink() {
23+
if (sinkAdded) {
24+
return;
25+
}
26+
27+
if (hasListeners(stream)) {
28+
return;
29+
}
30+
31+
sinkAdded = true;
32+
stream.pipe(sinkStream);
33+
}
34+
2135
function removeSink(evt) {
2236
if (evt !== 'readable' && evt !== 'data') {
2337
return;
2438
}
2539

2640
if (hasListeners(stream)) {
27-
stream.unpipe(sinkStream);
41+
sinkAdded = false;
42+
return stream.unpipe(sinkStream);
2843
}
2944
}
3045

3146
stream.on('newListener', removeSink);
3247
stream.on('removeListener', removeSink);
48+
stream.on('removeListener', addSink);
3349

34-
return function() {
35-
if (hasListeners(stream)) {
36-
return;
37-
}
38-
39-
stream.pipe(sinkStream);
40-
};
50+
return addSink;
4151
}
4252

4353
module.exports = sink;

test/dest.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,9 @@ describe('dest stream', function() {
14111411

14121412
srcStream.pipe(countFiles).pipe(destStream);
14131413

1414-
destStream.removeListener('readable', noop);
1414+
process.nextTick(function() {
1415+
destStream.removeListener('readable', noop);
1416+
});
14151417
});
14161418

14171419
it('sinks the stream if all the data event handlers are removed', function(done) {
@@ -1444,7 +1446,9 @@ describe('dest stream', function() {
14441446

14451447
srcStream.pipe(countFiles).pipe(destStream);
14461448

1447-
destStream.removeListener('data', onData);
1449+
process.nextTick(function() {
1450+
destStream.removeListener('data', onData);
1451+
});
14481452
});
14491453

14501454
it('should pass options to through2', function(done) {

0 commit comments

Comments
 (0)