Skip to content

Commit 14f1bdb

Browse files
committed
Add async iterable<T> type to WebIDL
This commit lifts the async iterator processing logic from the `ReadableStreamFromIterable` operation in the Streams spec, to a new WebIDL type `async iterable<T>`. This will clean up the streams spec, and enable a change in the Fetch spec to directly allow taking async iterable as request/response bodies, without having to pass them to `ReadableStream.from` first.
1 parent 402a886 commit 14f1bdb

File tree

1 file changed

+187
-6
lines changed

1 file changed

+187
-6
lines changed

index.bs

Lines changed: 187 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ urlPrefix: https://tc39.es/ecma262/; spec: ecmascript
6868
text: internal slot
6969
text: own property; url: sec-own-property
7070
text: PromiseCapability; url: sec-promisecapability-records
71+
text: Iterator; url: sec-iterator-records
7172
text: element size; url: table-the-typedarray-constructors
7273
urlPrefix: https://tc39.es/proposal-resizablearraybuffer/; spec: RESIZABLE-BUFFERS-PROPOSAL
7374
type: abstract-op
@@ -3350,6 +3351,7 @@ the following algorithm returns <i>true</i>.
33503351
<th><div><span>interface-like</span></div>
33513352
<th><div><span>callback function</span></div>
33523353
<th><div><span>dictionary-like</span></div>
3354+
<th><div><span>async iterable</span></div>
33533355
<th><div><span>sequence-like</span></div>
33543356
</thead>
33553357
<tr>
@@ -3365,6 +3367,7 @@ the following algorithm returns <i>true</i>.
33653367
<td>●
33663368
<td>
33673369
<td>●
3370+
<td>●
33683371
<tr>
33693372
<th>boolean
33703373
<td class="belowdiagonal">
@@ -3378,6 +3381,7 @@ the following algorithm returns <i>true</i>.
33783381
<td>●
33793382
<td>●
33803383
<td>●
3384+
<td>●
33813385
<tr>
33823386
<th>numeric types
33833387
<td class="belowdiagonal">
@@ -3391,6 +3395,7 @@ the following algorithm returns <i>true</i>.
33913395
<td>●
33923396
<td>●
33933397
<td>●
3398+
<td>●
33943399
<tr>
33953400
<th>bigint
33963401
<td class="belowdiagonal">
@@ -3404,6 +3409,7 @@ the following algorithm returns <i>true</i>.
34043409
<td>●
34053410
<td>●
34063411
<td>●
3412+
<td>●
34073413
<tr>
34083414
<th>string types
34093415
<td class="belowdiagonal">
@@ -3417,6 +3423,7 @@ the following algorithm returns <i>true</i>.
34173423
<td>●
34183424
<td>●
34193425
<td>●
3426+
<td>●
34203427
<tr>
34213428
<th>object
34223429
<td class="belowdiagonal">
@@ -3430,6 +3437,7 @@ the following algorithm returns <i>true</i>.
34303437
<td>
34313438
<td>
34323439
<td>
3440+
<td>
34333441
<tr>
34343442
<th>symbol
34353443
<td class="belowdiagonal">
@@ -3443,6 +3451,7 @@ the following algorithm returns <i>true</i>.
34433451
<td>●
34443452
<td>●
34453453
<td>●
3454+
<td>●
34463455
<tr>
34473456
<th>interface-like
34483457
<td class="belowdiagonal">
@@ -3456,6 +3465,7 @@ the following algorithm returns <i>true</i>.
34563465
<td>●
34573466
<td>●
34583467
<td>●
3468+
<td>●
34593469
<tr>
34603470
<th>callback function
34613471
<td class="belowdiagonal">
@@ -3469,6 +3479,7 @@ the following algorithm returns <i>true</i>.
34693479
<td>
34703480
<td>(c)
34713481
<td>●
3482+
<td>●
34723483
<tr>
34733484
<th>dictionary-like
34743485
<td class="belowdiagonal">
@@ -3482,6 +3493,21 @@ the following algorithm returns <i>true</i>.
34823493
<td class="belowdiagonal">
34833494
<td>
34843495
<td>●
3496+
<td>●
3497+
<tr>
3498+
<th>async iterable
3499+
<td class="belowdiagonal">
3500+
<td class="belowdiagonal">
3501+
<td class="belowdiagonal">
3502+
<td class="belowdiagonal">
3503+
<td class="belowdiagonal">
3504+
<td class="belowdiagonal">
3505+
<td class="belowdiagonal">
3506+
<td class="belowdiagonal">
3507+
<td class="belowdiagonal">
3508+
<td class="belowdiagonal">
3509+
<td>
3510+
<td>
34853511
<tr>
34863512
<th>sequence-like
34873513
<td class="belowdiagonal">
@@ -3494,6 +3520,7 @@ the following algorithm returns <i>true</i>.
34943520
<td class="belowdiagonal">
34953521
<td class="belowdiagonal">
34963522
<td class="belowdiagonal">
3523+
<td class="belowdiagonal">
34973524
<td>
34983525
</table>
34993526

@@ -4059,7 +4086,7 @@ The following extended attributes are applicable to [=iterable declarations=]:
40594086
</pre>
40604087

40614088

4062-
<h4 id="idl-async-iterable">Asynchronously iterable declarations</h4>
4089+
<h4 oldids="idl-async-iterable" id="idl-async-iterable-declaration">Asynchronously iterable declarations</h4>
40634090

40644091
An [=interface=] can be declared to be asynchronously iterable by using an
40654092
<dfn id="dfn-async-iterable-declaration" export>asynchronously iterable declaration</dfn>
@@ -5665,6 +5692,7 @@ are known as <dfn id="dfn-object-type" export>object types</dfn>.
56655692
StringType Null
56665693
identifier Null
56675694
"sequence" "&lt;" TypeWithExtendedAttributes "&gt;" Null
5695+
"async iterable" "&lt;" TypeWithExtendedAttributes "&gt;" Null
56685696
"object" Null
56695697
"symbol" Null
56705698
BufferRelatedType Null
@@ -6166,6 +6194,26 @@ sequence is used.
61666194
Any [=list=] can be implicitly treated as a <code>sequence&lt;|T|&gt;</code>, as long as it contains
61676195
only [=list/items=] that are of type |T|.
61686196

6197+
<!-- Note: if we ever add synchronous iterable types, we should add a note here about why sequences and iterables are not the same. -->
6198+
6199+
<h4 id="idl-async-iterable-type" lt="async-iterable" dfn export>Async iterable types — async iterable&lt;|T|&gt;</h4>
6200+
6201+
The <dfn lt="async iterable type" export>async iterable&lt;|T|&gt;</dfn> type is a parameterized
6202+
type whose values are references to objects that asynchronously yield zero or more values of type
6203+
|T|.
6204+
6205+
Async iterables, unlike sequences, do not have a fixed length and can be infinite. Values are
6206+
asynchronously produced as the async iterable is iterated over.
6207+
6208+
Async iterable are passed by reference in language bindings where they are represented by an object.
6209+
This means that passing an async iterable to a [=platform object=] will result in a reference to the
6210+
async iterable being kept by that object. Similarly, any async iterable returned from a platform
6211+
object will be a reference to the same object and modifications made to it will be visible to the
6212+
platform object. This is in contrast to sequences, which are always passed by value.
6213+
6214+
Async iterables must not be used as the type of an [=attribute=] or [=constant=].
6215+
6216+
There is no way to represent an async iterable value in IDL.
61696217

61706218
<h4 id="idl-record" lt="record" dfn export>Record types — record&lt;|K|, |V|&gt;</h4>
61716219

@@ -8064,6 +8112,122 @@ JavaScript Array values.
80648112
</div>
80658113

80668114

8115+
<h4 id="js-async-iterable">Async iterable — async iterable&lt;|T|&gt;</h4>
8116+
8117+
IDL <a lt="async iterable type">async iterable&lt;|T|&gt;</a> values are represented by JavaScript
8118+
[=Iterator=] records.
8119+
8120+
<div id="js-to-async-iterable" algorithm="convert a JavaScript value to async iterable">
8121+
A JavaScript value |V| is [=converted to an IDL value|converted=]
8122+
to an IDL <a lt="async iterable type">async iterable&lt;<var ignore>T</var>&gt;</a> value as follows:
8123+
8124+
1. If <a abstract-op>Type</a>(|V|) is not Object,
8125+
[=JavaScript/throw=] a <l spec=ecmascript>{{TypeError}}</l>.
8126+
1. Let |iteratorRecord| be [=?=] <a abstract-op>GetIterator</a>(|V|, async).
8127+
1. Return |iteratorRecord|.
8128+
</div>
8129+
8130+
An IDL <a lt="async iterable type">async iterable&lt;|T|&gt;</a> value can not be
8131+
[=converted to a JavaScript value=].
8132+
8133+
Note: Instead of returning an async iterable from an IDL operation, the operation should return an
8134+
[=interface=] that has an [=asynchronously iterable declaration=].
8135+
8136+
<h5 id="js-async-iterable-iteration">Iterating async iterables</h5>
8137+
8138+
<div algorithm>
8139+
8140+
To <dfn id="async-iterable-get-next-value" export lt="get an async iterable next value">get the next value</dfn> of an
8141+
<code><a lt="async iterable type">async iterable&lt;<var>T</var>&gt;</a></code> |iteratorRecord|,
8142+
perform the following steps:
8143+
8144+
1. Let |nextResult| be the result of calling <a abstract-op>IteratorNext</a>(|iteratorRecord|).
8145+
1. If |nextResult| is an abrupt completion, return [=a promise rejected with=]
8146+
|nextResult|.\[[Value]].
8147+
1. Let |nextPromise| be [=a promise resolved with=] |nextResult|.\[[Value]].
8148+
1. Return the result of [=reacting=] to |nextPromise| with with the following fulfillment
8149+
steps, given |iterResult|:
8150+
1. If <a abstract-op>Type</a>(|iterResult|) is not Object, [=JavaScript/throw=] a
8151+
<l spec=ecmascript>{{TypeError}}</l>.
8152+
1. Let |done| be [=?=] <a abstract-op>IteratorComplete</a>(|iterResult|).
8153+
1. If |done| is true:
8154+
1. Return [=end of iteration=].
8155+
1. Otherwise:
8156+
1. Let |V| be [=?=] <a abstract-op>IteratorValue</a>(|iterResult|).
8157+
1. Let |value| be the result of [=converted to an IDL value|converting=] |V| to an IDL
8158+
value of type |T|.
8159+
1. Return |value|.
8160+
8161+
</div>
8162+
8163+
<div algorithm>
8164+
8165+
To <dfn id="async-iterable-finish-iterating" export lt="finish iterating an async iterable">finish iterating</dfn> an
8166+
<code><a lt="async iterable type">async iterable&lt;<var ignore>T</var>&gt;</a></code> |iteratorRecord|,
8167+
perform the following steps:
8168+
8169+
1. Let |iterator| be |iteratorRecord|.\[[Iterator]].
8170+
1. Let |returnMethod| be <a abstract-op>GetMethod</a>(|iterator|, "return").
8171+
1. If |returnMethod| is an abrupt completion, return [=a promise rejected with=]
8172+
|returnMethod|.\[[Value]].
8173+
1. If |returnMethod| is <emu-val>undefined</emu-val>, return [=a promise resolved with=]
8174+
<emu-val>undefined</emu-val>.
8175+
1. Let |returnResult| be <a abstract-op>Call</a>(|returnMethod|.\[[Value]], |iterator|).
8176+
1. If |returnResult| is an abrupt completion, return [=a promise rejected with=]
8177+
|returnResult|.\[[Value]].
8178+
1. Let |returnPromise| be [=a promise resolved with=] |returnResult|.\[[Value]].
8179+
1. Return the result of [=reacting=] to |returnPromise| with the following fulfillment steps,
8180+
given |returnPromiseResult|:
8181+
1. If <a abstract-op>Type</a>(|returnPromiseResult|) is not Object, [=JavaScript/throw=] a
8182+
<l spec=ecmascript>{{TypeError}}</l>.
8183+
1. Return <emu-val>undefined</emu-val>.
8184+
8185+
</div>
8186+
8187+
<div class="example" id="example-js-async-iterable">
8188+
8189+
<code>concatN</code> is an [=operation=] that returns a promise that will be fulfilled with the
8190+
concatenation of all the strings yielded by the async iterable passed to it. It stops
8191+
concatenating and finishes the iterator once the async iterable has yielded N strings.
8192+
8193+
<pre>
8194+
interface I {
8195+
Promise&lt;DOMString> concat(async iterable&lt;DOMString> strings, unsigned long maxN);
8196+
};
8197+
</pre>
8198+
8199+
<div algorithm="concatN">
8200+
8201+
The <code>concatN(|iterable|, |maxN|)</code> method steps are:
8202+
8203+
1. Let |promise| be [=a new promise=].
8204+
1. Let |result| be the empty string.
8205+
1. Let |n| be 0.
8206+
1. Let |step| be a sequence of steps that will be used to process the async iterable:
8207+
1. Let |next| be the result of <a lt="get an async iterable next value">getting the next value</a> of |iterable|.
8208+
1. [=React=] to |next|:
8209+
- If |next| was fulfilled with value |v|:
8210+
1. If |v| is [=end of iteration=], [=resolve=] |promise| with |result|.
8211+
1. Set |result| to the result of concatenating |result| and |v|.
8212+
1. Set |n| to |n| + 1.
8213+
1. If |n| is |maxN|, then:
8214+
1. Let |finish| be the result of <a lt="finish iterating an async iterable">finishing iterating</a> |iterable|.
8215+
1. [=React=] to |finish|:
8216+
- If |finish| was fulfilled, [=resolve=] |promise| with |result|.
8217+
- If |finish| was rejected with reason |r|, [=reject=] |promise| with |r|.
8218+
1. Otherwise:
8219+
1. Call |step|.
8220+
- If |next| was rejected with reason |r|, [=reject=] |promise| with |r|.
8221+
1. Call step.
8222+
1. Return |promise|.
8223+
8224+
</div>
8225+
</div>
8226+
8227+
8228+
</div>
8229+
8230+
80678231
<h4 id="js-record" oldids="es-record">Records — record&lt;|K|, |V|&gt;</h4>
80688232

80698233
IDL [=record=]&lt;|K|, |V|&gt; values are represented by
@@ -11174,6 +11338,23 @@ Note: The HTML Standard defines how a security check is performed. [[!HTML]]
1117411338
1. Otherwise: if <a abstract-op>Type</a>(|V|) is Object and
1117511339
there is an entry in |S| that has one of the
1117611340
following types at position |i| of its type list,
11341+
* a [=async iterable type=]
11342+
* a [=nullable type|nullable=] version of any of the above types
11343+
* an [=annotated type=] whose [=annotated types/inner type=] is one of the above types
11344+
* a [=union type=], [=nullable type|nullable=] union type, or [=annotated type|annotated=] union type
11345+
that has one of the above types in its [=flattened member types=]
11346+
11347+
and after performing the following steps,
11348+
11349+
1. Let |method| be [=?=] <a abstract-op>GetMethod</a>(|V|, {{@@asyncIterator}}).
11350+
11351+
|method| is not <emu-val>undefined</emu-val>, then remove from |S| all
11352+
other entries.
11353+
11354+
1. Otherwise: if <a abstract-op>Type</a>(|V|) is Object and
11355+
there is an entry in |S| that has one of the
11356+
following types at position |i| of its type list,
11357+
* a [=async iterable type=]
1117711358
* a [=sequence type=]
1117811359
* a [=frozen array type=]
1117911360
* a [=nullable type|nullable=] version of any of the above types
@@ -11342,11 +11523,11 @@ Note: The HTML Standard defines how a security check is performed. [[!HTML]]
1134211523
Generally, the inspection of the value at the distinguishing argument index does not have any
1134311524
side effects, and the only side effects in the overload resolution algorithm are the result of
1134411525
converting the JavaScript values to IDL values.
11345-
(An exception exists when one of the overloads has a [=sequence type=] or [=frozen array type=]
11346-
at the distinguishing argument index.
11347-
In this case, we attempt to get the {{@@iterator}} property to determine the appropriate
11348-
overload, and perform the conversion of the distinguishing argument separately before continuing
11349-
with the next step.)
11526+
(An exception exists when one of the overloads has a [=async iterable type=], [=sequence type=]
11527+
or [=frozen array type=] at the distinguishing argument index.
11528+
In this case, we attempt to get the {{@@asyncIterator}} / {{@@iterator}} property to determine
11529+
the appropriate overload, and perform the conversion of the distinguishing argument separately
11530+
before continuing with the next step.)
1135011531

1135111532
At this point, we have determined which overload to use. We now
1135211533
convert the remaining arguments, from the distinguishing argument onwards,

0 commit comments

Comments
 (0)