diff options
author | Robert Nagy <ronagy@icloud.com> | 2020-01-05 18:41:31 +0100 |
---|---|---|
committer | Robert Nagy <ronagy@icloud.com> | 2020-02-08 23:23:12 +0100 |
commit | e559842188f541b884abff2ffad4d2d3e1b841a6 (patch) | |
tree | 43efbf696e890e1cf6ae5f851cfe30e420f3c179 /lib/_stream_readable.js | |
parent | 9c753b3dc2b465496a94d7ccffc3c9438c3ce325 (diff) | |
download | node-new-e559842188f541b884abff2ffad4d2d3e1b841a6.tar.gz |
stream: make readable & writable computed
This makes readable and writable automatically computed based
on the stream state.
Effectivly deprecating/discouraging manual management of this.
Makes the properties more consistent and easier to reason about.
Fixes: https://github.com/nodejs/node/issues/29377
PR-URL: https://github.com/nodejs/node/pull/31197
Refs: https://github.com/nodejs/node/issues/29377
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/_stream_readable.js')
-rw-r--r-- | lib/_stream_readable.js | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 6fa3540057..4c64fd1b4f 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -23,6 +23,7 @@ const { ArrayIsArray, + Boolean, NumberIsInteger, NumberIsNaN, ObjectDefineProperties, @@ -181,9 +182,6 @@ function Readable(options) { this._readableState = new ReadableState(options, this, isDuplex); - // legacy - this.readable = true; - if (options) { if (typeof options.read === 'function') this._read = options.read; @@ -1057,6 +1055,20 @@ Readable.prototype[SymbolAsyncIterator] = function() { // because otherwise some prototype manipulation in // userland will fail ObjectDefineProperties(Readable.prototype, { + readable: { + get() { + const r = this._readableState; + if (!r) return false; + if (r.readable !== undefined) return r.readable && !r.endEmitted; + return Boolean(!r.destroyed && !r.errorEmitted && !r.endEmitted); + }, + set(val) { + // Backwards compat. + if (this._readableState) { + this._readableState.readable = !!val; + } + } + }, readableHighWaterMark: { enumerable: false, @@ -1198,7 +1210,6 @@ function endReadableNT(state, stream) { // Check that we didn't get one last unshift. if (!state.errorEmitted && !state.endEmitted && state.length === 0) { state.endEmitted = true; - stream.readable = false; stream.emit('end'); if (state.autoDestroy) { |