[whatwg/streams] Tracking issue for Web IDL async iterator support (#1017)

A blocker for #963 is expanding Web IDL's async iterator support to be powerful enough for readable streams. The tracking issue over there is https://github.com/heycam/webidl/issues/800.

On this side I want to record a draft of the algorithms. After the following get done:

- https://github.com/heycam/webidl/pull/802
- https://github.com/heycam/webidl/issues/804
- https://github.com/heycam/webidl/issues/803
- The actual async iterator PR tracked in https://github.com/heycam/webidl/issues/800

then it will look something like this:

**Async iterator initialization steps**, given a `ReadableStream` _stream_, async iterator _iterator_, and argument _options_:

1. Let _reader_ be ? AcquireReadableStreamDefaultReader(_stream_).
1. Set _iterator_.[[reader]] to _reader_.
1. Set _iterator_.[[preventCancel]] to _options_["`preventCancel`"].

To **get the next iteration result** for `ReadableStream` given the async iterator _iterator_:

1. Let promise be a new promise.
1. Let _reader_ be _iterator_.[[reader]].
1. If _reader_.[[ownerReadableStream]] is undefined, return a promise rejected with a `TypeError` exception.
1. Return the result of reacting to ! ReadableStreamDefaultReaderRead(_reader_) with a fulfillment handler that takes an argument _result_ and performs the following steps:
    1. Assert: Type(_result_) is Object.
    1. Let done be ! Get(_result_, "`done`").
    1. Assert: Type(_done_) is Boolean.
    1. If _done_ is true,
        1. Perform ! ReadableStreamReaderGenericRelease(_reader_).
        1. Return undefined.
    1. Let _value_ be ! Get(_result_, "`value`").
    1. Return _value.

(The above will become a bit cleaner if we are able to move away from using actual ES objects for read results, which will likely happen as part of the general Web IDL conversion.)

To **close the async iterator** for `ReadableStream` given the async iterator _iterator_ and argument _value_:

1. Let _reader_ be _iterator_.[[asyncIteratorReader]].
1. If _reader_.[[ownerReadableStream]] is undefined, return a promise rejected with a `TypeError` exception.
1. If _reader_.[[readRequests]] is not empty, return a promise rejected with a `TypeError` exception.
1. If _iterator_.[[preventCancel]] is false, then:
    1. Let _result_ be ! ReadableStreamReaderGenericCancel(_reader_, _value_).
    1. Perform ! ReadableStreamReaderGenericRelease(_reader_).
    1. Return _result_.
1. Perform ! ReadableStreamReaderGenericRelease(reader).
1. Return a promise resolved with undefined.

---

I think this all works, so that's good.

-- 
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/1017

Received on Tuesday, 24 September 2019 09:30:35 UTC