Skip to content

Commit f693318

Browse files
committed
Tweak async iterator algorithms
Make both the "async iterator initialization steps" and the "get the next iteration result" take two arguments: the instance, and the async iterator. This ends up being more ergonomic than using the "current state" concept.
1 parent 4203404 commit f693318

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

index.bs

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,19 +4302,17 @@ and {{@@asyncIterator}} properties on its [=interface prototype object=].
43024302

43034303
Prose accompanying an [=interface=] with an [=asynchronously iterable declaration=] must define a
43044304
<dfn id="dfn-get-the-next-iteration-result">get the next iteration result</dfn> algorithm.
4305-
This algorithm receives a <b>[=this=]</b> value, which is an instance of the [=interface=] that it
4306-
is defined for, and the <dfn export>current state</dfn>.
4305+
This algorithm receives the instance of the [=interface=] that is being iterated, as well as the
4306+
async iterator itself (which can be useful for storing state).
43074307
It must return a {{Promise}} that either resolves with undefined – to signal the end of the
4308-
iteration – or a tuple with three elements:
4308+
iteration – or a tuple with two elements:
43094309

43104310
1. a value of the first type given in the declaration;
4311-
1. a value of the second type given in the declaration;
4312-
1. an opaque value that is passed back to the next invocation of the algorithm as the
4313-
<b>[=current state=]</b>.
4311+
1. a value of the second type given in the declaration.
43144312

43154313
The prose may also define <dfn>asynchronous iterator initialization steps</dfn> for the
43164314
[=interface=] with an [=asynchronously iterable declaration=], which would then be called with the
4317-
newly created iterator object.
4315+
instance of the [=interface=] being iterated, as well as the newly created iterator object.
43184316

43194317
[=Interfaces=] with an [=asynchronously iterable declaration=] must not have any
43204318
[=interface members=] named "<code>entries</code>", "<code>keys</code>", or "<code>values</code>",
@@ -4344,26 +4342,35 @@ or have any [=inherited interfaces=] that have [=interface members=] with these
43444342

43454343
<blockquote>
43464344

4347-
To [=get the next iteration result=] for <code class="idl">SessionManager</code>, run the
4348-
following steps:
4345+
The [=asynchronous iterator initialization steps=] for a <code class="idl">SessionManager</code>
4346+
async iterator |iterator| are:
4347+
4348+
1. Set |iterator|'s <dfn for="SessionManager async iterator">current state</dfn> to "not
4349+
yet started".
4350+
4351+
To [=get the next iteration result=] for a <code class="idl">SessionManager</code>
4352+
|manager| and its async iterator |iterator|:
43494353

43504354
1. Let |promise| be a new promise.
43514355
1. Let |key| be the following value, if it exists, or null otherwise:
43524356
<dl class="switch">
4353-
: If <b>current state</b> is "not yet started"
4354-
:: the smallest username in <b>this</b>'s open sessions, in lexicographical order
4357+
: If |iterator|'s [=SessionManager async iterator/current state=] is "not yet
4358+
started"
4359+
:: the smallest username in |manager|'s open sessions, in lexicographical order
43554360

43564361
: Otherwise
4357-
:: the smallest username in <b>this</b>'s open sessions that is greater than
4358-
<b>current state</b>, in lexicographical order
4362+
:: the smallest username in |manager|'s open sessions that is greater than
4363+
|iterator|'s current state, in lexicographical order
43594364
</dl>
43604365

4361-
Note: <b>current state</b> might no longer be present in the open sessions.
4366+
Note: |iterator|'s [=SessionManager async iterator/current state=] might no longer be
4367+
present in the open sessions.
43624368
1. If |key| is null, then:
43634369
1. Resolve |promise| with undefined.
43644370
1. Otherwise:
43654371
1. Let |session| be the <code class="idl">Session</code> object corresponding to |key|.
4366-
1. Resolve |promise| with (|username|, |session|, |username|).
4372+
1. Resolve |promise| with (|username|, |session|).
4373+
1. Set |iterator|'s [=SessionManager async iterator/current state=] to |username|.
43674374
1. Return |promise|.
43684375

43694376
</blockquote>
@@ -12115,10 +12122,11 @@ The location of the property is determined as follows:
1211512122
1. If |object| does not [=implement=] |interface|, then [=ECMAScript/throw=] a
1211612123
{{ECMAScript/TypeError}}.
1211712124
1. Let |iterator| be a newly created [=default asynchronous iterator object=] for |interface|
12118-
with |object| as its [=default asynchronous iterator object/target=] and
12119-
"<code>key+value</code>" as its [=default asynchronous iterator object/kind=].
12120-
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |iterator|, if
12121-
any.
12125+
with |object| as its [=default asynchronous iterator object/target=],
12126+
"<code>key+value</code>" as its [=default asynchronous iterator object/kind=], and
12127+
[=default asynchronous iterator object/is finished=] set to false.
12128+
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |object| and
12129+
|iterator|, if any such steps exist.
1212212130
1. Return |iterator|.
1212312131
</div>
1212412132

@@ -12305,10 +12313,11 @@ then the [=function object=] is {{%ArrayProto_keys%}}.
1230512313
1. If |object| does not [=implement=] |interface|, then [=ECMAScript/throw=] a
1230612314
{{ECMAScript/TypeError}}.
1230712315
1. Let |iterator| be a newly created [=default asynchronous iterator object=] for |interface|
12308-
with |object| as its [=default asynchronous iterator object/target=] and
12309-
"<code>key</code>" as its [=default asynchronous iterator object/kind=].
12310-
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |iterator|, if
12311-
any.
12316+
with |object| as its [=default asynchronous iterator object/target=],
12317+
"<code>key</code>" as its [=default asynchronous iterator object/kind=], and
12318+
[=default asynchronous iterator object/is finished=] set to false.
12319+
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |object| and
12320+
|iterator|, if any such steps exist.
1231212321
1. Return |iterator|.
1231312322
</div>
1231412323

@@ -12369,10 +12378,11 @@ the value of the {{@@iterator}} property.
1236912378
1. If |object| does not [=implement=] |interface|, then [=ECMAScript/throw=] a
1237012379
{{ECMAScript/TypeError}}.
1237112380
1. Let |iterator| be a newly created [=default asynchronous iterator object=] for |interface|
12372-
with |object| as its [=default asynchronous iterator object/target=] and
12373-
"<code>value</code>" as its [=default asynchronous iterator object/kind=].
12374-
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |iterator|, if
12375-
any.
12381+
with |object| as its [=default asynchronous iterator object/target=],
12382+
"<code>value</code>" as its [=default asynchronous iterator object/kind=], and
12383+
[=default asynchronous iterator object/is finished=] set to false.
12384+
1. Run the [=asynchronous iterator initialization steps=] for |interface| with |object| and
12385+
|iterator|, if any such steps exist.
1237612386
1. Return |iterator|.
1237712387
</div>
1237812388

@@ -12495,16 +12505,7 @@ A [=default asynchronous iterator object=] has internal values:
1249512505
* its <dfn for="default asynchronous iterator object">kind</dfn>, which is the iteration kind,
1249612506
* its <dfn for="default asynchronous iterator object">ongoing promise</dfn>, which is a
1249712507
{{Promise}} or undefined,
12498-
* its <dfn for="default asynchronous iterator object">state</dfn>, which is one of
12499-
"not yet started" and "finished", or an opaque value used to store the position of the iterator
12500-
by the algorithm to [=get the next iteration result=].
12501-
12502-
Note: This value is associated with the iterator instance, rather than the [=interface=]
12503-
instance. It is explicitly handled here because the iterator instance is not exposed to the
12504-
prose that [=get the next iteration result|gets the next iteration result=].
12505-
12506-
When a [=default asynchronous iterator object=] is first created, its
12507-
[=default asynchronous iterator object/state=] is "not yet started".
12508+
* its <dfn for="default asynchronous iterator object">is finished</dfn>, which is a boolean.
1250812509

1250912510
Note: [=Default asynchronous iterator objects=] do not have [=class strings=]; when
1251012511
<code class="idl">Object.prototype.toString()</code> is called on a
@@ -12559,8 +12560,7 @@ The \[[Prototype]] [=internal slot=] of an [=asynchronous iterator prototype obj
1255912560

1256012561
1. Let |nextSteps| be the following steps:
1256112562
1. Let |nextPromiseCapability| be [=!=] [$NewPromiseCapability$]({{%Promise%}}).
12562-
1. Let |oldState| be |object|'s [=default asynchronous iterator object/state=].
12563-
1. If |oldState| is "finished", then:
12563+
1. If |object|'s [=default asynchronous iterator object/is finished=] is true, then:
1256412564
1. Let |result| be [$CreateIterResultObject$](<emu-val>undefined</emu-val>,
1256512565
<emu-val>true</emu-val>).
1256612566
1. Perform [=!=] [$Call$](|nextPromiseCapability|.\[[Resolve]],
@@ -12569,20 +12569,18 @@ The \[[Prototype]] [=internal slot=] of an [=asynchronous iterator prototype obj
1256912569
1. Let |kind| be |object|'s [=default asynchronous iterator object/kind=].
1257012570
1. Let |nextPromise| be the result of
1257112571
[=get the next iteration result|getting the next iteration result=] with |object|'s
12572-
[=default asynchronous iterator object/target=] as <b>[=this=]</b> and |oldState| as
12573-
the <b>[=current state=]</b>.
12572+
[=default asynchronous iterator object/target=] and |object|.
1257412573
1. Let |resolveSteps| be the following steps, given |next|:
1257512574
1. Set |object|'s [=default asynchronous iterator object/ongoing promise=] to
1257612575
undefined.
1257712576
1. If |next| is undefined, then:
12578-
1. Set |object|'s [=default asynchronous iterator object/state=] to null.
12577+
1. Set |object|'s [=default asynchronous iterator object/is finished=] to true.
1257912578
1. Let |result| be [$CreateIterResultObject$](<emu-val>undefined</emu-val>,
1258012579
<emu-val>true</emu-val>).
1258112580
1. Perform [=!=] [$Call$](|nextPromiseCapability|.\[[Resolve]],
1258212581
<emu-val>undefined</emu-val>, « |result| »).
1258312582
1. Otherwise:
12584-
1. Let (|key|, |value|, |newState|) be |next|.
12585-
1. Set |object|'s [=default asynchronous iterator object/state=] to |newState|.
12583+
1. Let (|key|, |value|) be |next|.
1258612584
1. Let |result| be the [=iterator result=] for (|key|, |value|) and |kind|.
1258712585
1. Perform [=!=] [$Call$](|nextPromiseCapability|.\[[Resolve]],
1258812586
<emu-val>undefined</emu-val>, « |result| »).

0 commit comments

Comments
 (0)