Skip to content

Commit 38c50c4

Browse files
annevkdomenic
andcommitted
Editorial: refactor and explain spin the event loop
This makes the algorithm more explicit, with properly-nested in parallel and task queuing steps. In particular, it makes it clear (with examples) how it is a "spec macro". Co-Authored-By: Anne van Kesteren <annevk@annevk.nl> Co-Authored-By: Domenic Denicola <d@domenic.me>
1 parent fd330a2 commit 38c50c4

File tree

1 file changed

+149
-10
lines changed

1 file changed

+149
-10
lines changed

source

Lines changed: 149 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90045,7 +90045,8 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
9004590045

9004690046
<hr>
9004790047

90048-
<p>When an algorithm says to <dfn>spin the event loop</dfn> until a condition <var>goal</var> is met, the user agent must run the following steps:</p>
90048+
<p>Algorithm steps that say to <dfn>spin the event loop</dfn> until a condition <var>goal</var> is
90049+
met are equivalent to substituting in the following algorithm steps:</p>
9004990050

9005090051
<ol>
9005190052
<li>
@@ -90071,23 +90072,161 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
9007190072
</li>
9007290073

9007390074
<li>
90074-
<p>Stop <var>task</var>, allowing whatever algorithm that invoked it to resume, but continue
90075-
these steps <span>in parallel</span>.</p>
90075+
<p><span>In parallel</span>:</p>
90076+
90077+
<ol>
90078+
<li><p>Wait until the condition <var>goal</var> is met.</p></li>
90079+
90080+
<li>
90081+
<p><span>Queue a task</span> on <var>task source</var> to:</p>
90082+
90083+
<ol>
90084+
<li><p>Replace the <span>JavaScript execution context stack</span> with <var>old
90085+
stack</var>.</p></li>
90086+
90087+
<li>
90088+
<p>Perform any steps that appear after this <span>spin the event loop</span> instance in the
90089+
original algorithm.</p>
90090+
90091+
<p class="note">This resumes <var>task</var>.</p>
90092+
</li>
90093+
</ol>
90094+
</li>
90095+
</ol>
90096+
</li>
90097+
90098+
<li>
90099+
<p>Stop <var>task</var>, allowing whatever algorithm that invoked it to resume.</p>
9007690100

9007790101
<p class="note">This causes the <span>event loop</span>'s main set of steps or the <span>perform
9007890102
a microtask checkpoint</span> algorithm to continue.</p>
9007990103
</li>
90104+
</ol>
9008090105

90081-
<li><p>Wait until the condition <var>goal</var> is met.</p></li>
90106+
<p class="note">Unlike other algorithms in this and other specifications, which behave similar to
90107+
programming-language function calls, <span>spin the event loop</span> is more like a macro, which
90108+
saves typing and indentation at the usage site by expanding into a series of steps and
90109+
operations.</p>
9008290110

90083-
<li><p><span>Queue a task</span> to continue running these steps, on <var>task source</var>. Wait
90084-
until this new task runs before continuing these steps.</p></li>
90111+
<div class="example">
90112+
<p>An algorithm whose steps are:</p>
9008590113

90086-
<li><p>Replace the <span>JavaScript execution context stack</span> with the <var>old
90087-
stack</var>.</p></li>
90114+
<ol>
90115+
<li><p>Do something.</p></li>
9008890116

90089-
<li><p>Return to the caller.</p></li>
90090-
</ol>
90117+
<li><p><span>Spin the event loop</span> until awesomeness happens.</p></li>
90118+
90119+
<li><p>Do something else.</p></li>
90120+
</ol>
90121+
90122+
<p>is a shorthand which, after "macro expansion", becomes</p>
90123+
90124+
<ol>
90125+
<li><p>Do something.</p></li>
90126+
90127+
<li><p>Let <var>old stack</var> be a copy of the <span>JavaScript execution context
90128+
stack</span>.</p></li>
90129+
90130+
<li><p>Empty the <span>JavaScript execution context stack</span>.</p></li>
90131+
90132+
<li><p><span>Perform a microtask checkpoint</span>.</p></li>
90133+
90134+
<li>
90135+
<p><span>In parallel</span>:</p>
90136+
90137+
<ol>
90138+
<li><p>Wait until awesomeness happens.</p></li>
90139+
90140+
<li>
90141+
<p><span>Queue a task</span> on the task source in which "do something" was done to:</p>
90142+
90143+
<ol>
90144+
<li><p>Replace the <span>JavaScript execution context stack</span> with <var>old
90145+
stack</var>.</p></li>
90146+
90147+
<li><p>Do something else.</p></li>
90148+
</ol>
90149+
</li>
90150+
</ol>
90151+
</li>
90152+
</ol>
90153+
</div>
90154+
90155+
<div class="example">
90156+
<p>Here is a more full example of the substitution, where the event loop is spun from inside a
90157+
task that is queued from work in parallel. The version using <span>spin the event loop</span>:</p>
90158+
90159+
<ol>
90160+
<li>
90161+
<p><span>In parallel</span>:</p>
90162+
90163+
<ol>
90164+
<li><p>Do parallel thing 1.</p></li>
90165+
90166+
<li>
90167+
<p><span>Queue a task</span> on the <span class="no-backref">DOM manipulation task
90168+
source</span> to:</p>
90169+
90170+
<ol>
90171+
<li><p>Do task thing 1.</p></li>
90172+
90173+
<li><p><span>Spin the event loop</span> until awesomeness happens.</p></li>
90174+
90175+
<li><p>Do task thing 2.</p></li>
90176+
</ol>
90177+
</li>
90178+
90179+
<li><p>Do parallel thing 2.</p></li>
90180+
</ol>
90181+
</li>
90182+
</ol>
90183+
90184+
<p>The fully expanded version:</p>
90185+
90186+
<ol>
90187+
<li>
90188+
<p><span>In parallel</span>:</p>
90189+
90190+
<ol>
90191+
<li><p>Do parallel thing 1.</p></li>
90192+
90193+
<li><p>Let <var>old stack</var> be null.</p></li>
90194+
90195+
<li>
90196+
<p><span>Queue a task</span> on the <span class="no-backref">DOM manipulation task
90197+
source</span> to:</p>
90198+
90199+
<ol>
90200+
<li><p>Do task thing 1.</p></li>
90201+
90202+
<li><p>Set <var>old stack</var> to a copy of the <span>JavaScript execution context
90203+
stack</span>.</p></li>
90204+
90205+
<li><p>Empty the <span>JavaScript execution context stack</span>.</p></li>
90206+
90207+
<li><p><span>Perform a microtask checkpoint</span>.</p></li>
90208+
</ol>
90209+
</li>
90210+
90211+
<li><p>Wait until awesomeness happens.</p></li>
90212+
90213+
<li>
90214+
<p><span>Queue a task</span> on the <span class="no-backref">DOM manipulation task
90215+
source</span> to:</p>
90216+
90217+
<ol>
90218+
<li><p>Replace the <span>JavaScript execution context stack</span> with <var>old
90219+
stack</var>.</p></li>
90220+
90221+
<li><p>Do task thing 2.</p></li>
90222+
</ol>
90223+
</li>
90224+
90225+
<li><p>Do parallel thing 2.</p></li>
90226+
</ol>
90227+
</li>
90228+
</ol>
90229+
</div>
9009190230

9009290231
<hr>
9009390232

0 commit comments

Comments
 (0)