Skip to content

Commit a293112

Browse files
committed
lib: clarify maybeReadMore_ loop conditions
1 parent a7cc7a3 commit a293112

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

lib/_stream_readable.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,30 @@ function maybeReadMore(stream, state) {
569569

570570
function maybeReadMore_(stream, state) {
571571
var len = state.length;
572+
573+
// Attempt to read more data if we should.
574+
//
575+
// The conditions for reading more data are (one of):
576+
// - Not enough data buffered (state.length < state.highWaterMark). The loop
577+
// is responsible for filling the buffer with enough data if such data
578+
// is available. If highWaterMark is 0 and we are not in the flowing mode
579+
// we should _not_ attempt to buffer any extra data. We'll get more data
580+
// when the stream consumer calls read() instead.
581+
// - No data in the buffer, and the stream is in flowing mode. In this mode
582+
// the loop below is responsible for ensuring read() is called. Failing to
583+
// call read here would abort the flow and there's no other mechanism for
584+
// continuing the flow if the stream consumer has just subscribed to the
585+
// 'data' event.
586+
//
587+
// In addition to the above conditions to keep reading data, the following
588+
// conditions prevent the data from being read:
589+
// - The stream has ended (state.ended).
590+
// - There is already a pending 'read' operation (state.reading). This is a
591+
// case where the the stream has called the implementation defined _read()
592+
// method, but they are processing the call asynchronously and have _not_
593+
// called push() with new data. In this case we skip performing more
594+
// read()s. The execution ends in this method again after the _read() ends
595+
// up calling push() with more data.
572596
while (!state.reading && !state.ended &&
573597
(state.length < state.highWaterMark ||
574598
(state.flowing && state.length === 0))) {

0 commit comments

Comments
 (0)