@@ -28,6 +28,7 @@ const {
28
28
ObjectDefineProperty,
29
29
ObjectSetPrototypeOf,
30
30
SymbolAsyncIterator,
31
+ Symbol
31
32
} = primordials ;
32
33
33
34
module . exports = Readable ;
@@ -51,6 +52,8 @@ const {
51
52
ERR_STREAM_UNSHIFT_AFTER_END_EVENT
52
53
} = require ( 'internal/errors' ) . codes ;
53
54
55
+ const kPaused = Symbol ( 'kPaused' ) ;
56
+
54
57
// Lazy loaded to improve the startup performance.
55
58
let StringDecoder ;
56
59
let createReadableStreamAsyncIterator ;
@@ -127,7 +130,7 @@ function ReadableState(options, stream, isDuplex) {
127
130
this . emittedReadable = false ;
128
131
this . readableListening = false ;
129
132
this . resumeScheduled = false ;
130
- this . paused = true ;
133
+ this [ kPaused ] = null ;
131
134
132
135
// Should close be emitted on destroy. Defaults to true.
133
136
this . emitClose = ! options || options . emitClose !== false ;
@@ -159,6 +162,16 @@ function ReadableState(options, stream, isDuplex) {
159
162
}
160
163
}
161
164
165
+ // Legacy property for `paused`
166
+ ObjectDefineProperty ( ReadableState . prototype , 'paused' , {
167
+ get ( ) {
168
+ return this [ kPaused ] !== false ;
169
+ } ,
170
+ set ( value ) {
171
+ this [ kPaused ] = ! ! value ;
172
+ }
173
+ } ) ;
174
+
162
175
function Readable ( options ) {
163
176
if ( ! ( this instanceof Readable ) )
164
177
return new Readable ( options ) ;
@@ -348,7 +361,8 @@ function chunkInvalid(state, chunk) {
348
361
349
362
350
363
Readable . prototype . isPaused = function ( ) {
351
- return this . _readableState . flowing === false ;
364
+ const state = this . _readableState ;
365
+ return state [ kPaused ] === true || state . flowing === false ;
352
366
} ;
353
367
354
368
// Backwards compatibility.
@@ -947,14 +961,16 @@ function updateReadableListening(self) {
947
961
const state = self . _readableState ;
948
962
state . readableListening = self . listenerCount ( 'readable' ) > 0 ;
949
963
950
- if ( state . resumeScheduled && ! state . paused ) {
964
+ if ( state . resumeScheduled && state [ kPaused ] === false ) {
951
965
// Flowing needs to be set to true now, otherwise
952
966
// the upcoming resume will not flow.
953
967
state . flowing = true ;
954
968
955
969
// Crude way to check if we should resume
956
970
} else if ( self . listenerCount ( 'data' ) > 0 ) {
957
971
self . resume ( ) ;
972
+ } else if ( ! state . readableListening ) {
973
+ state . flowing = null ;
958
974
}
959
975
}
960
976
@@ -975,7 +991,7 @@ Readable.prototype.resume = function() {
975
991
state . flowing = ! state . readableListening ;
976
992
resume ( this , state ) ;
977
993
}
978
- state . paused = false ;
994
+ state [ kPaused ] = false ;
979
995
return this ;
980
996
} ;
981
997
@@ -1006,7 +1022,7 @@ Readable.prototype.pause = function() {
1006
1022
this . _readableState . flowing = false ;
1007
1023
this . emit ( 'pause' ) ;
1008
1024
}
1009
- this . _readableState . paused = true ;
1025
+ this . _readableState [ kPaused ] = true ;
1010
1026
return this ;
1011
1027
} ;
1012
1028
0 commit comments