mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.17k stars 22.46k forks source link

[Web API] Implement async iteration of ReadableStream #23678

Closed Rumyra closed 1 year ago

Rumyra commented 1 year ago

Acceptance Criteria

For folks helping with Firefox related documentation

Features to document

Updates to DOM streams

Related Gecko bugs

https://bugzilla.mozilla.org/show_bug.cgi?id=1734244

Other

hamishwillee commented 1 year ago

Updated: OK. I have a reasonable idea of the main usage. But trying to confirm the right way to cancel a stream that is being read in this way, to handle errors, and to polyfill, in


This one is going to be tricky because I'm not great with async, iterables, or streams. This is initial research + brain dump.

  1. The spec section of interest is here: https://streams.spec.whatwg.org/#rs-asynciterator
  2. The spec has normative info which says the

    for await (const chunk of stream) { ... }
    for await (const chunk of stream.values({ [preventCancel](https://streams.spec.whatwg.org/#dom-readablestreamiteratoroptions-preventcancel): true })) { ... }

    Asynchronously iterates over the chunks in the stream’s internal queue.

    Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator’s return() method is called, e.g. by breaking out of the loop.

    By default, calling the async iterator’s return() method will also cancel the stream. To prevent this, use the stream’s values() method, passing true for the preventCancel option.

    • So now you can basically consume the chunks of a stream, returning a promise each time that you can wait on/
    • You can also throw to get out of it, and return done when done.
  3. The interface (IDL here now includes:
    async iterable<any>(optional ReadableStreamIteratorOptions options = {});

    where options is an optional object that may define the boolean preventCancel with default value false.

There are a bunch of things I am not sure of.

hamishwillee commented 1 year ago

Status:

hamishwillee commented 1 year ago

@Rumyra What I have done so far is enough for a release - release note, basic update to ReadableStream, and BCD.

However this is not yet done- I still need to update "Using Readable streams. I have most of my runnable test code in place, but that will be a separate PR. Anything to do with streams is a bit challenging.

hamishwillee commented 1 year ago

OK, tried to finish this off. I'll close this if it gets through reviews.