Re: [whatwg/streams] `ReadableStreamDefaultReader.read()` return values (#1117)

The underlying source [enqueues](https://streams.spec.whatwg.org/#rs-default-controller-enqueue) a bunch of chunks. If the stream's reader does not immediately `read()` these, they stay in the stream's queue. So when the underlying source [closes](https://streams.spec.whatwg.org/#rs-default-controller-close) the stream, the reader doesn't see this until it has read *all* of the previously enqueued chunks. Afterwards, the reader observes the stream being closed: its next `read()` returns `{ done: true }` and its `closed` promise resolves.

My terminology might have been a bit inaccurate. When the underlying source calls `close()`, this sets the "close requested" flag. After the last chunk is read from the queue, the stream's state becomes "closed". So you're right: when the stream is in the "closed" state, that always implies that all chunks have been read from the queue. Sorry for the confusion!

And yes, only the reader can cancel the stream. If you call `reader.cancel()`, the stream will immediately throw away all queued chunks and resolve any pending read with `{ done: true }`. But since `readAllChunks()` never calls `cancel()`, the only way a read can become `done` is if the stream becomes closed.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1117#issuecomment-808113678

Received on Friday, 26 March 2021 10:46:28 UTC