Skip to content

Commit ea765eb

Browse files
ronagTrott
authored andcommitted
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: #29155 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent cc7cec2 commit ea765eb

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

lib/_stream_readable.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,9 @@ function howMuchToRead(n, state) {
386386
else
387387
return state.length;
388388
}
389-
// If we're asking for more than the current hwm, then raise the hwm.
390-
if (n > state.highWaterMark)
391-
state.highWaterMark = computeNewHighWaterMark(n);
392389
if (n <= state.length)
393390
return n;
394-
// Don't have enough
395-
if (!state.ended) {
396-
state.needReadable = true;
397-
return 0;
398-
}
399-
return state.length;
391+
return state.ended ? state.length : 0;
400392
}
401393

402394
// You can override either this method, or the async _read(n) below.
@@ -412,6 +404,10 @@ Readable.prototype.read = function(n) {
412404
const state = this._readableState;
413405
const nOrig = n;
414406

407+
// If we're asking for more than the current hwm, then raise the hwm.
408+
if (n > state.highWaterMark)
409+
state.highWaterMark = computeNewHighWaterMark(n);
410+
415411
if (n !== 0)
416412
state.emittedReadable = false;
417413

0 commit comments

Comments
 (0)