Commit 5167080 1 parent ef0b8e0 commit 5167080 Copy full SHA for 5167080
File tree 1 file changed +23
-2
lines changed
1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
var Writable = require ( 'readable-stream/writable' ) ;
4
4
5
+ function listenerCount ( stream , evt ) {
6
+ return stream . listeners ( evt ) . length ;
7
+ }
8
+
5
9
function sink ( stream ) {
6
10
var sinkStream = new Writable ( {
7
11
objectMode : true ,
@@ -10,9 +14,26 @@ function sink(stream) {
10
14
} ,
11
15
} ) ;
12
16
17
+ stream . on ( 'removeListener' , function ( ) {
18
+ if ( listenerCount ( stream , 'readable' ) || listenerCount ( stream , 'data' ) ) {
19
+ stream . unpipe ( sinkStream ) ;
20
+ }
21
+ } ) ;
22
+
23
+ stream . on ( 'unpipe' , function ( ) {
24
+ if ( ! ( listenerCount ( stream , 'readable' ) || listenerCount ( stream , 'data' ) ) ) {
25
+ stream . pipe ( sinkStream ) ;
26
+ }
27
+ } ) ;
28
+
29
+ stream . on ( 'newListener' , function ( ) {
30
+ if ( listenerCount ( stream , 'readable' ) || listenerCount ( stream , 'data' ) ) {
31
+ stream . unpipe ( sinkStream ) ;
32
+ }
33
+ } ) ;
34
+
13
35
return function ( ) {
14
- // Respect readable listeners on the underlying stream
15
- if ( stream . listeners ( 'readable' ) . length > 0 ) {
36
+ if ( listenerCount ( stream , 'readable' ) || listenerCount ( stream , 'data' ) ) {
16
37
return ;
17
38
}
18
39
You can’t perform that action at this time.
0 commit comments