Skip to content

Commit 1e3e6da

Browse files
ronagtargos
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 17319e7 commit 1e3e6da

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

lib/_stream_readable.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,9 @@ function howMuchToRead(n, state) {
377377
else
378378
return state.length;
379379
}
380-
// If we're asking for more than the current hwm, then raise the hwm.
381-
if (n > state.highWaterMark)
382-
state.highWaterMark = computeNewHighWaterMark(n);
383380
if (n <= state.length)
384381
return n;
385-
// Don't have enough
386-
if (!state.ended) {
387-
state.needReadable = true;
388-
return 0;
389-
}
390-
return state.length;
382+
return state.ended ? state.length : 0;
391383
}
392384

393385
// You can override either this method, or the async _read(n) below.
@@ -403,6 +395,10 @@ Readable.prototype.read = function(n) {
403395
const state = this._readableState;
404396
const nOrig = n;
405397

398+
// If we're asking for more than the current hwm, then raise the hwm.
399+
if (n > state.highWaterMark)
400+
state.highWaterMark = computeNewHighWaterMark(n);
401+
406402
if (n !== 0)
407403
state.emittedReadable = false;
408404

0 commit comments

Comments
 (0)