Skip to content

Commit

Permalink
Make event loops 1:1 with agents
Browse files Browse the repository at this point in the history
Event loops ought to be 1:1 with agents, let's make it so.

Helps with #4198.
  • Loading branch information
annevk committed Nov 28, 2018
1 parent 44da679 commit 11c75f2
Showing 1 changed file with 37 additions and 58 deletions.
95 changes: 37 additions & 58 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -77492,11 +77492,6 @@ console.assert(iframeWindow.frameElement === null);
any other group. Each such group is a <dfn data-export="">unit of related similar-origin browsing
contexts</dfn>.</p>

<p class="note">There is also at most one <span>event loop</span> per <span>unit of related
similar-origin browsing contexts</span> (though several <span data-x="unit of related
similar-origin browsing contexts">units of related similar-origin browsing contexts</span> can
have a shared <span>event loop</span>).</p>

</div>


Expand Down Expand Up @@ -78978,13 +78973,9 @@ interface <dfn>BarProp</dfn> {
</dd>

<dt>The <span>responsible event loop</span></dt>
<dd>

<p>Return the <span>event loop</span> that is associated with the <span>unit of related
similar-origin browsing contexts</span> to which <var>window</var>'s <span>browsing
context</span> belongs.</p>

</dd>
<dd><p>Return the <span>event loop</span> associated with the <span>agent</span> whose set of
<span data-x="JavaScript realm">realms</span> contains <var>window</var>'s <span>relevant
Realm</span>.</p></dd>

<dt>The <span>responsible document</span></dt>
<dd>
Expand Down Expand Up @@ -88680,7 +88671,7 @@ import "https://example.com/foo/../module2.mjs";</code></pre>
<p class="&#x0058;&#x0058;&#x0058;">JavaScript is expected to define <span
data-x="agent">agents</span> in more detail; in particular that they hold a set of <span
data-x="concept-global-object-realm">realms</span>: <a
href="https://github.com/tc39/ecma262/issues/882">tc39/ecma262 issue #882</a>.</p>
href="https://github.com/tc39/ecma262/issues/1357">tc39/ecma262 issue #1357</a>.</p>

<p class="note">Conceptually, the <span>agent</span> concept is an architecture-independent,
idealized "thread" in which JavaScript code runs. Such code can involve multiple globals/<span
Expand Down Expand Up @@ -89119,27 +89110,11 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {

<p>To coordinate events, user interaction, scripts, rendering, networking, and so forth, user
agents must use <dfn data-x="event loop" data-lt="event loop" data-export="">event loops</dfn> as
described in this section. There are two kinds of event loops: those for <span
data-x="browsing context">browsing contexts</span>, and those for <a
href="#workers">workers</a>.</p>
described in this section. Each <span>agent</span> has one <span>event loop</span>.

<p>There must be at least one <span>browsing context</span> <span>event loop</span> per user
agent, and at most one per <span>unit of related similar-origin browsing contexts</span>.</p>

<p class="note">When there is more than one <span>event loop</span> for a <span>unit of related
browsing contexts</span>, complications arise when a <span>browsing context</span> in that group
is <span data-x="navigate">navigated</span> such that it switches from one <span>unit of related
similar-origin browsing contexts</span> to another. This specification does not currently describe
how to handle these complications.</p>

<p>A <span>browsing context</span> <span>event loop</span> always has at least one <span>browsing
context</span>. If such an <span>event loop</span>'s <span data-x="browsing context">browsing
contexts</span> all go away, then the <span>event loop</span> goes away as well. A <span>browsing
context</span> always has an <span>event loop</span> coordinating its activities.</p>

<p><a href="#worker-event-loop">Worker event loops</a> are simpler: each worker has one
<span>event loop</span>, and the <span data-x="run a worker">worker processing model</span>
manages the <span>event loop</span>'s lifetime.</p>
<p class="note">This specification does not currently describe how to handle the complications
arising from <span data-x="navigate">navigating</span> between <span data-x="similar-origin window
agent">similar-origin window agents</span>.</p>

<hr>

Expand Down Expand Up @@ -89187,26 +89162,27 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {

</dl>

<p>Each <span data-x="concept-task">task</span> in a <span>browsing context</span> <span>event
loop</span> is associated with a <code>Document</code>; if the task was queued in the context of
an element, then it is the element's <span>node document</span>; if the task was queued in the context
of a <span>browsing context</span>, then it is the <span>browsing context</span>'s <span>active
document</span> at the time the task was queued; if the task was queued by or for a <span
data-x="concept-script">script</span> then the document is the <span>responsible document</span>
specified by the script's <span>settings object</span>.</p>
<p>Each <span data-x="concept-task">task</span> in an <span>event loop</span> for a
<span>similar-origin window agent</span> is associated with a <code>Document</code>; if the task
was queued in the context of an element, then it is the element's <span>node document</span>; if
the task was queued in the context of a <span>browsing context</span>, then it is the
<span>browsing context</span>'s <span>active document</span> at the time the task was queued; if
the task was queued by or for a <span data-x="concept-script">script</span> then the document is
the <span>responsible document</span> specified by the script's <span>settings object</span>.</p>

<p>A <span data-x="concept-task">task</span> is intended for a specific <span>event loop</span>:
the <span>event loop</span> that is handling <span data-x="concept-task">tasks</span> for the
<span data-x="concept-task">task</span>'s associated <code>Document</code> or <a
href="#workers">worker</a>.</p>
<!-- TODO: worklets? -->

<p>When a user agent is to <dfn data-export="">queue a task</dfn>, it must add the given task to
one of the <span data-x="task queue">task queues</span> of the relevant <span>event
loop</span>.</p>

<p>Each <span data-x="concept-task">task</span> is defined as coming from a specific <dfn
data-export="">task source</dfn>. All the tasks from one particular <span>task source</span> and
destined to a particular <span>event loop</span> (e.g. the callbacks generated by timers of a
destined to a particular <span>event loop</span> (e.g., the callbacks generated by timers of a
<code>Document</code>, the events fired for mouse movements over that <code>Document</code>, the
tasks queued for the parser of that <code>Document</code>) must always be added to the same
<span>task queue</span>, but <span data-x="concept-task">tasks</span> from different <span
Expand Down Expand Up @@ -89241,10 +89217,10 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
<!-- lots of places in the spec refer to "step 1" -->
<li id="step1"><p>Let <var>oldestTask</var> be the oldest <span data-x="concept-task">task</span>
on one of the <span>event loop</span>'s <span data-x="task queue">task queues</span>, if any,
ignoring, in the case of a <span>browsing context</span> <span>event loop</span>, tasks whose
associated <code>Document</code>s are not <span>fully active</span>. The user agent may pick any
<span>task queue</span>. If there is no task to select, then jump to the <i>microtasks</i> step
below.</p></li>
ignoring, in the case of a <span>similar-origin window agent</span> <span>event loop</span>,
tasks whose associated <code>Document</code>s are not <span>fully active</span>. The user agent
may pick any <span>task queue</span>. If there is no task to select, then jump to the
<i>microtasks</i> step below.</p></li>

<li>
<p>Report the duration of time during which the user agent does not execute this loop by
Expand Down Expand Up @@ -89296,9 +89272,8 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
</li>

<li>
<p><dfn data-export="">Update the rendering</dfn>: If this <span>event loop</span> is a
<span>browsing context</span> <span>event loop</span> (as opposed to a <a
href="#workers">worker</a> <span>event loop</span>), then run the following substeps.</p>
<p><dfn data-export="">Update the rendering</dfn>: if this <span>event loop</span> is for a
<span>similar-origin window agent</span>, then:</p>

<ol>
<li>
Expand Down Expand Up @@ -89422,14 +89397,13 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
</ol>
</li>

<li><p>If this is a <span>browsing context</span> <span>event loop</span> (as opposed to a <a
href="#workers">worker</a> <span>event loop</span>), and there are no <span
data-x="concept-task">tasks</span> in the <span>event loop</span>'s <span data-x="task
queue">task queues</span>, and the <span>event loop</span>'s <span>microtask queue</span> is
empty, and none of the <span data-x="browsing context">browsing contexts</span> have a
<span>rendering opportunity</span>, then for each <span>browsing context</span>, run the steps in
the <span>start an idle period algorithm</span>, passing the <code>Window</code> associated with
that <span>browsing context</span>. <ref spec="REQUESTIDLECALLBACK"></p></li>
<li><p>If this is an <span>event loop</span> for a <span>similar-origin window agent</span>, and
there are no <span data-x="concept-task">tasks</span> in the <span>event loop</span>'s <span
data-x="task queue">task queues</span>, and the <span>event loop</span>'s <span>microtask
queue</span> is empty, and none of the <span data-x="browsing context">browsing contexts</span>
have a <span>rendering opportunity</span>, then for each <span>browsing context</span>, run the
steps in the <span>start an idle period algorithm</span>, passing the <code>Window</code>
associated with that <span>browsing context</span>. <ref spec="REQUESTIDLECALLBACK"></p></li>

<li>
<p>Report the duration of the <span>update the rendering</span> step by performing the
Expand All @@ -89450,14 +89424,16 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {
</li>

<li>
<p>If this is a <a href="#workers">worker</a> <span>event loop</span> (i.e., one running for
a <code>WorkerGlobalScope</code>):</p>
<p>If this is an <span>event loop</span> that is not for a <span>similar-origin window
agent</span> or <span>worklet agent</span> (e.g., one for a <span>dedicated worker
agent</span>):</p>

<ol>
<li>
<p>If this is a <span data-x="concept-AnimationFrameProvider-supported">supported</span>
<code>DedicatedWorkerGlobalScope</code> and the user agent believes that it would benefit from
having its rendering updated at this time, then:</p>
<!-- TODO: do we need to grab this global from the agent now? -->

<ol>
<li><p>Let <var>now</var> be the <span>current high resolution time</span>. <ref
Expand Down Expand Up @@ -97890,6 +97866,9 @@ interface <dfn>SharedWorkerGlobalScope</dfn> : <span>WorkerGlobalScope</span> {

<h4 id="worker-event-loop">The event loop</h4>

<!-- TODO: should we create an "agent mixin" for workers that we can use here? Should "closing"
move there? -->

<p>Each <code>WorkerGlobalScope</code> object has a distinct <span>event loop</span>, separate
from those used by <span data-x="unit of related similar-origin browsing contexts">units of related
similar-origin browsing contexts</span>. This <span>event loop</span> has no associated
Expand Down

0 comments on commit 11c75f2

Please sign in to comment.