@@ -223,6 +223,7 @@ Readable.prototype.unshift = function(chunk) {
223
223
} ;
224
224
225
225
function readableAddChunk ( stream , chunk , encoding , addToFront , skipChunkCheck ) {
226
+ debug ( 'readableAddChunk' , chunk ) ;
226
227
var state = stream . _readableState ;
227
228
if ( chunk === null ) {
228
229
state . reading = false ;
@@ -799,20 +800,24 @@ Readable.prototype.unpipe = function(dest) {
799
800
// Ensure readable listeners eventually get something
800
801
Readable . prototype . on = function ( ev , fn ) {
801
802
const res = Stream . prototype . on . call ( this , ev , fn ) ;
803
+ const state = this . _readableState ;
802
804
803
805
if ( ev === 'data' ) {
804
- // Start flowing on next tick if stream isn't explicitly paused
805
- if ( this . _readableState . flowing !== false )
806
+ // update readableListening so that resume() may be a no-op
807
+ // a few lines down. This is needed to support once('readable').
808
+ state . readableListening = this . listenerCount ( 'readable' ) > 0 ;
809
+
810
+ // Try start flowing on next tick if stream isn't explicitly paused
811
+ if ( state . flowing !== false )
806
812
this . resume ( ) ;
807
813
} else if ( ev === 'readable' ) {
808
- const state = this . _readableState ;
809
814
if ( ! state . endEmitted && ! state . readableListening ) {
810
815
state . readableListening = state . needReadable = true ;
811
816
state . emittedReadable = false ;
812
- if ( ! state . reading ) {
813
- process . nextTick ( nReadingNextTick , this ) ;
814
- } else if ( state . length ) {
817
+ if ( state . length ) {
815
818
emitReadable ( this ) ;
819
+ } else if ( ! state . reading ) {
820
+ process . nextTick ( nReadingNextTick , this ) ;
816
821
}
817
822
}
818
823
}
@@ -821,6 +826,42 @@ Readable.prototype.on = function(ev, fn) {
821
826
} ;
822
827
Readable . prototype . addListener = Readable . prototype . on ;
823
828
829
+ Readable . prototype . removeListener = function ( ev , fn ) {
830
+ const res = Stream . prototype . removeListener . call ( this , ev , fn ) ;
831
+
832
+ if ( ev === 'readable' ) {
833
+ // We need to check if there is someone still listening to
834
+ // to readable and reset the state. However this needs to happen
835
+ // after readable has been emitted but before I/O (nextTick) to
836
+ // support once('readable', fn) cycles. This means that calling
837
+ // resume within the same tick will have no
838
+ // effect.
839
+ process . nextTick ( updateReadableListening , this ) ;
840
+ }
841
+
842
+ return res ;
843
+ } ;
844
+
845
+ Readable . prototype . removeAllListeners = function ( ev ) {
846
+ const res = Stream . prototype . removeAllListeners . call ( this , ev ) ;
847
+
848
+ if ( ev === 'readable' || ev === undefined ) {
849
+ // We need to check if there is someone still listening to
850
+ // to readable and reset the state. However this needs to happen
851
+ // after readable has been emitted but before I/O (nextTick) to
852
+ // support once('readable', fn) cycles. This means that calling
853
+ // resume within the same tick will have no
854
+ // effect.
855
+ process . nextTick ( updateReadableListening , this ) ;
856
+ }
857
+
858
+ return res ;
859
+ } ;
860
+
861
+ function updateReadableListening ( self ) {
862
+ self . _readableState . readableListening = self . listenerCount ( 'readable' ) > 0 ;
863
+ }
864
+
824
865
function nReadingNextTick ( self ) {
825
866
debug ( 'readable nexttick read 0' ) ;
826
867
self . read ( 0 ) ;
@@ -832,7 +873,9 @@ Readable.prototype.resume = function() {
832
873
var state = this . _readableState ;
833
874
if ( ! state . flowing ) {
834
875
debug ( 'resume' ) ;
835
- state . flowing = true ;
876
+ // we flow only if there is no one listening
877
+ // for readable
878
+ state . flowing = ! state . readableListening ;
836
879
resume ( this , state ) ;
837
880
}
838
881
return this ;
0 commit comments