| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1463214 commit bca23b9
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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; | |
@@ -126,7 +129,7 @@ function ReadableState(options, stream, isDuplex) { | |||
| 126 | 129 | this.emittedReadable = false; | |
| 127 | 130 | this.readableListening = false; | |
| 128 | 131 | this.resumeScheduled = false; | |
| 129 | - this.paused = true; | ||
| 132 | + this[kPaused] = null; | ||
| 130 | 133 | ||
| 131 | 134 | // True if the error was already emitted and should not be thrown again | |
| 132 | 135 | this.errorEmitted = false; | |
@@ -170,6 +173,16 @@ ObjectDefineProperty(ReadableState.prototype, 'pipesCount', { | |||
| 170 | 173 | } | |
| 171 | 174 | }); | |
| 172 | 175 | ||
| 176 | + // Legacy property for `paused` | ||
| 177 | + ObjectDefineProperty(ReadableState.prototype, 'paused', { | ||
| 178 | + get() { | ||
| 179 | + return this[kPaused] !== false; | ||
| 180 | + }, | ||
| 181 | + set(value) { | ||
| 182 | + this[kPaused] = !!value; | ||
| 183 | + } | ||
| 184 | + }); | ||
| 185 | + | ||
| 173 | 186 | function Readable(options) { | |
| 174 | 187 | if (!(this instanceof Readable)) | |
| 175 | 188 | return new Readable(options); | |
@@ -365,7 +378,8 @@ function chunkInvalid(state, chunk) { | |||
| 365 | 378 | ||
| 366 | 379 | ||
| 367 | 380 | Readable.prototype.isPaused = function() { | |
| 368 | - return this._readableState.flowing === false; | ||
| 381 | + const state = this._readableState; | ||
| 382 | + return state[kPaused] === true || state.flowing === false; | ||
| 369 | 383 | }; | |
| 370 | 384 | ||
| 371 | 385 | // Backwards compatibility. | |
@@ -962,14 +976,16 @@ function updateReadableListening(self) { | |||
| 962 | 976 | const state = self._readableState; | |
| 963 | 977 | state.readableListening = self.listenerCount('readable') > 0; | |
| 964 | 978 | ||
| 965 | - if (state.resumeScheduled && !state.paused) { | ||
| 979 | + if (state.resumeScheduled && state[kPaused] === false) { | ||
| 966 | 980 | // Flowing needs to be set to true now, otherwise | |
| 967 | 981 | // the upcoming resume will not flow. | |
| 968 | 982 | state.flowing = true; | |
| 969 | 983 | ||
| 970 | 984 | // Crude way to check if we should resume | |
| 971 | 985 | } else if (self.listenerCount('data') > 0) { | |
| 972 | 986 | self.resume(); | |
| 987 | + } else if (!state.readableListening) { | ||
| 988 | + state.flowing = null; | ||
| 973 | 989 | } | |
| 974 | 990 | } | |
| 975 | 991 | ||
@@ -990,7 +1006,7 @@ Readable.prototype.resume = function() { | |||
| 990 | 1006 | state.flowing = !state.readableListening; | |
| 991 | 1007 | resume(this, state); | |
| 992 | 1008 | } | |
| 993 | - state.paused = false; | ||
| 1009 | + state[kPaused] = false; | ||
| 994 | 1010 | return this; | |
| 995 | 1011 | }; | |
| 996 | 1012 | ||
@@ -1021,7 +1037,7 @@ Readable.prototype.pause = function() { | |||
| 1021 | 1037 | this._readableState.flowing = false; | |
| 1022 | 1038 | this.emit('pause'); | |
| 1023 | 1039 | } | |
| 1024 | - this._readableState.paused = true; | ||
| 1040 | + this._readableState[kPaused] = true; | ||
| 1025 | 1041 | return this; | |
| 1026 | 1042 | }; | |
| 1027 | 1043 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + | ||
| 4 | + const { Readable } = require('stream'); | ||
| 5 | + | ||
| 6 | + const readable = new Readable({ | ||
| 7 | + read() {} | ||
| 8 | + }); | ||
| 9 | + | ||
| 10 | + function read() {} | ||
| 11 | + | ||
| 12 | + readable.setEncoding('utf8'); | ||
| 13 | + readable.on('readable', read); | ||
| 14 | + readable.removeListener('readable', read); | ||
| 15 | + | ||
| 16 | + process.nextTick(function() { | ||
| 17 | + readable.on('data', common.mustCall()); | ||
| 18 | + readable.push('hello'); | ||
| 19 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const common = require('../common'); | |
| 4 | + const assert = require('assert'); | ||
| 4 | 5 | const { Readable } = require('stream'); | |
| 5 | 6 | ||
| 6 | 7 | let ticks = 18; | |
@@ -38,3 +39,20 @@ function readAndPause() { | |||
| 38 | 39 | ||
| 39 | 40 | rs.on('data', ondata); | |
| 40 | 41 | } | |
| 42 | + | ||
| 43 | + { | ||
| 44 | + const readable = new Readable({ | ||
| 45 | + read() {} | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + function read() {} | ||
| 49 | + | ||
| 50 | + readable.setEncoding('utf8'); | ||
| 51 | + readable.on('readable', read); | ||
| 52 | + readable.removeListener('readable', read); | ||
| 53 | + readable.pause(); | ||
| 54 | + | ||
| 55 | + process.nextTick(function() { | ||
| 56 | + assert(readable.isPaused()); | ||
| 57 | + }); | ||
| 58 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments