Skip to content

Commit

Permalink
Add promise version setTimeout with AbortSignal support
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Dec 6, 2020
1 parent 009a731 commit 856b8eb
Showing 1 changed file with 106 additions and 29 deletions.
135 changes: 106 additions & 29 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<dfn data-x="concept-element-is-value" data-x-href="https://dom.spec.whatwg.org/#concept-element-is-value"><code data-x="">is</code> value</dfn></li>

<li><dfn data-x-href="https://dom.spec.whatwg.org/#mutationobserver"><code>MutationObserver</code></dfn> interface and <dfn data-x-href="https://dom.spec.whatwg.org/#mutation-observers">mutation observers</dfn> in general</li>
<li><dfn data-x-href="https://dom.spec.whatwg.org/#interface-AbortSignal"><code>AbortSignal</code></dfn> interface, its <dfn data-x-href="https://dom.spec.whatwg.org/#abortsignal-aborted-flag">aborted flag</dfn>, and the concept of <dfn data-x="concept-abortsignal-add" data-x-href="https://dom.spec.whatwg.org/#abortsignal-add">add</dfn></li>
</ul>

<p>The following features are defined in <cite>UI Events</cite>: <ref spec=UIEVENTS></p>
Expand Down Expand Up @@ -93042,20 +93043,8 @@ enum <dfn>DOMParserSupportedType</dfn> {
<span>in parallel</span>.</p></li>

<li>
<p>If <var>method context</var> is a <code>Window</code> object, wait until the
<code>Document</code> associated with <var>method context</var> has been <span>fully
active</span> for a further <var>timeout</var> milliseconds (not necessarily
consecutively).</p>

<p>Otherwise, <var>method context</var> is a <code>WorkerGlobalScope</code> object;
wait until <var>timeout</var> milliseconds have passed with the worker not suspended
(not necessarily consecutively).</p>
</li>

<li>
<p>Wait until any invocations of this algorithm that had the same <var>method
context</var>, that started before this one, and whose <var>timeout</var> is equal to
or less than this one's, have completed.</p>
<p>Run <span>timer queuing steps</span>, providing <var>method context</var> and
<var>task</var>.</p>

<p class="note">Argument conversion as defined by Web IDL (for example, invoking <code
data-x="">toString()</code> methods on objects passed as the first argument) happens in the
Expand All @@ -93075,21 +93064,6 @@ setTimeout({ toString: function () {
} }, 100);</code></pre>

</div>
</li>

<li>
<p>Optionally, wait a further <span>implementation-defined</span> length of time.</p>

<p class="note">This is intended to allow user agents to pad timeouts as needed to optimize the
power usage of the device. For example, some processors have a low-power mode where the
granularity of timers is reduced; on such platforms, user agents can slow timers down to fit
this schedule instead of requiring the processor to use the more accurate mode with its
associated higher power usage.</p>
</li>

<li>
<p><span>Queue a global task</span> on the <dfn>timer task source</dfn> given <var>method
context</var> to run <var>task</var>.</p>

<p class="note">Once the task has been processed, if the <var>repeat</var> flag is
false, it is safe to remove the entry for <var>handle</var> from the <span>list of
Expand Down Expand Up @@ -93130,6 +93104,109 @@ scheduleWork(); // queues a task to do lots of work</code></pre>
</div>


<h3 id="timers-namespace">The <code>timers</code> namespace</h3>

<p>The <code>timers</code> namespace is for promise-based timer functions.</p>

<pre><code class="idl">[Exposed=(Window,Worker)]
namespace <dfn>timers</dfn> {
Promise&lt;undefined&gt; <span data-x="dom-timers-setTimeout">setTimeout</span>(optional long timeout = 0, optional <span>TimerOptions</span> options = {});
};

dictionary <dfn>TimerOptions</dfn> {
<span>AbortSignal</span> <dfn data-x="dom-TimerOptions-signal">signal</dfn>;
};</code></pre>

<dl class="domintro">
<dt>timers . <code subdfn data-x="dom-timers-setTimeout">setTimeout</code>( <var>timeout</var> [, <var>options</var> ] )</dt>
<dd><p>Returns a promise that resolves after the given timeout.</p></dd>
</dl>

<p>The <dfn data-x="dom-timers-setTimeout"><code
id="dom-timers-setTimeout">setTimeout(<var>message</var>, <var>options</var>)</code></dfn> method,
when invoked, must run the following steps:</p>

<ol>
<li><p>Let <var>p</var> be a new promise.</p></li>

<li><p>Let <var>signal</var> be <var>options</var>["<code
data-x="dom-TimerOptions-signal">signal</code>"].</p></li>

<li>
<p>If <var>signal</var> is not undefined, then:</p>
<ol>
<li><p>If its <span>aborted flag</span> is set, then
reject <var>p</var> with an <span>"<code>AbortError</code>"</span> <code>DOMException</code>
and return <var>p</var>.</p></li>

<li>
<p><span data-x="concept-abortsignal-add">Add</span> the following abort steps to it:</p>

<ol>
<li><p>Reject <var>p</var> with an <span>"<code>AbortError</code>"</span>
<code>DOMException</code>.</p></li>
</ol>
</li>
</ol>
</li>

<li>
<p>Let <var>task</var> be a <span data-x="concept-task">task</span> that runs the following
substeps:</p>

<ol>
<li><p>Resolve <var>p</var>.</p></li>
</ol>
</li>

<li><p>If <var>timeout</var> is less than 0, then set <var>timeout</var> to 0.</p></li>

<li><p>Return <var>p</var>, and then continue running this algorithm <span>in
parallel</span>.</p></li>

<li><p>Run <span>timer queuing steps</span>, providing the object on which the method for which
the algorithm is running is implemented (a <code>Window</code> or <code>WorkerGlobalScope</code>
object) as the <var>method context</var>, and <var>task</var>.</p></li>
</ol>

<p>The <dfn>timer queuing steps</dfn>, which are invoked with a <var>method context</var> and a
<var>task</var>, are as follows:</p>

<ol>
<li>
<p>If <var>method context</var> is a <code>Window</code> object, wait until the
<code>Document</code> associated with <var>method context</var> has been <span>fully
active</span> for a further <var>timeout</var> milliseconds (not necessarily
consecutively).</p>

<p>Otherwise, <var>method context</var> is a <code>WorkerGlobalScope</code> object;
wait until <var>timeout</var> milliseconds have passed with the worker not suspended
(not necessarily consecutively).</p>
</li>

<li>
<p>Wait until any invocations of this algorithm that had the same <var>method
context</var>, that started before this one, and whose <var>timeout</var> is equal to
or less than this one's, have completed.</p>
</li>

<li>
<p>Optionally, wait a further <span>implementation-defined</span> length of time.</p>

<p class="note">This is intended to allow user agents to pad timeouts as needed to optimize the
power usage of the device. For example, some processors have a low-power mode where the
granularity of timers is reduced; on such platforms, user agents can slow timers down to fit
this schedule instead of requiring the processor to use the more accurate mode with its
associated higher power usage.</p>
</li>

<li>
<p><span>Queue a global task</span> on the <dfn>timer task source</dfn> given <var>method
context</var> to run <var>task</var>.</p>
</li>
</ol>


<h3>Microtask queuing</h3>

<dl class="domintro">
Expand Down

0 comments on commit 856b8eb

Please sign in to comment.