stream: simplify howMuchToRead()
This slightly refactors read by moving side effects out of howMuchToRead(). We don't actually have to set state.needReadable = true; in howMuchToRead() since read handles 0 return as needReadable. PR-URL: https://github.com/nodejs/node/pull/29155 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
cc7cec25c5
commit
ea765eba20
@ -386,17 +386,9 @@ function howMuchToRead(n, state) {
|
|||||||
else
|
else
|
||||||
return state.length;
|
return state.length;
|
||||||
}
|
}
|
||||||
// If we're asking for more than the current hwm, then raise the hwm.
|
|
||||||
if (n > state.highWaterMark)
|
|
||||||
state.highWaterMark = computeNewHighWaterMark(n);
|
|
||||||
if (n <= state.length)
|
if (n <= state.length)
|
||||||
return n;
|
return n;
|
||||||
// Don't have enough
|
return state.ended ? state.length : 0;
|
||||||
if (!state.ended) {
|
|
||||||
state.needReadable = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return state.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// You can override either this method, or the async _read(n) below.
|
// You can override either this method, or the async _read(n) below.
|
||||||
@ -412,6 +404,10 @@ Readable.prototype.read = function(n) {
|
|||||||
const state = this._readableState;
|
const state = this._readableState;
|
||||||
const nOrig = n;
|
const nOrig = n;
|
||||||
|
|
||||||
|
// If we're asking for more than the current hwm, then raise the hwm.
|
||||||
|
if (n > state.highWaterMark)
|
||||||
|
state.highWaterMark = computeNewHighWaterMark(n);
|
||||||
|
|
||||||
if (n !== 0)
|
if (n !== 0)
|
||||||
state.emittedReadable = false;
|
state.emittedReadable = false;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user