From 1e3e6da9b9bdab5ab3a5678ce9d3d178e62444b3 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 16 Aug 2019 10:16:23 +0200 Subject: [PATCH] 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 Reviewed-By: Rich Trott --- lib/_stream_readable.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 0d23014760741b..47c177346b7d99 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -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. @@ -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;