Skip to content

Commit

Permalink
stream: simplify howMuchToRead()
Browse files Browse the repository at this point in the history
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: #29155
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ronag authored and targos committed Aug 20, 2019
1 parent 17319e7 commit 1e3e6da
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,9 @@ function howMuchToRead(n, state) {
else
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)
return n;
// Don't have enough
if (!state.ended) {
state.needReadable = true;
return 0;
}
return state.length;
return state.ended ? state.length : 0;
}

// You can override either this method, or the async _read(n) below.
Expand All @@ -403,6 +395,10 @@ Readable.prototype.read = function(n) {
const state = this._readableState;
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)
state.emittedReadable = false;

Expand Down

0 comments on commit 1e3e6da

Please sign in to comment.