Skip to content

Commit

Permalink
Implement AbortSignal.timeout
Browse files Browse the repository at this point in the history
AbortSignal.timeout is a static method that creates a new AbortSignal
that is automatically aborted after a specified duration. The
implementation is essentially PostDelayedTask(SignalAbort, ms).

Throttling: this API is specced to use the timer task source, but there
are three internally due to our throttling implementation. We use
immediate for the timeout == 0 case and high nesting for timeount > 0
(the typical case), i.e. all non-zero timeouts are eligible for
throttling (Note: this matches scheduler.postTask()).

Spec PR: whatwg/dom#1032
I2P: https://groups.google.com/a/chromium.org/g/blink-dev/c/9Y290P1WimY/m/bru989iAAgAJ

Bug: 1181925
Change-Id: I192d82a8bf12c368abcd47ae6c50e80f50654cf9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3425124
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Mason Freed <masonf@chromium.org>
Commit-Queue: Scott Haseley <shaseley@chromium.org>
Cr-Commit-Position: refs/heads/main@{#966406}
  • Loading branch information
shaseley authored and mattwoodrow committed Feb 15, 2022
1 parent b186808 commit 2036b08
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions dom/abort/AbortSignal.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,31 @@ async_test(t => {
s.onabort = t.unreached_func("abort event handler called");
t.step_timeout(() => { t.done(); }, 2000);
}, "signal returned by AbortSignal.abort() should not fire abort event");

test(t => {
const signal = AbortSignal.timeout(0);
assert_true(signal instanceof AbortSignal, "returned object is an AbortSignal");
assert_false(signal.aborted, "returned signal is not already aborted");
}, "AbortSignal.timeout() returns a non-aborted signal");

async_test(t => {
const signal = AbortSignal.timeout(5);
signal.onabort = t.step_func_done(() => {
assert_true(signal.aborted, "signal is aborted");
assert_true(signal.reason instanceof DOMException, "signal.reason is a DOMException");
assert_equals(signal.reason.name, "TimeoutError", "signal.reason is a TimeoutError");
});
}, "Signal returned by AbortSignal.timeout() times out");

async_test(t => {
let result = "";
for (const value of ["1", "2", "3"]) {
const signal = AbortSignal.timeout(5);
signal.onabort = t.step_func(() => { result += value; });
}

const signal = AbortSignal.timeout(5);
signal.onabort = t.step_func_done(() => {
assert_equals(result, "123", "Timeout order should be 123");
});
}, "AbortSignal timeouts fire in order");
19 changes: 19 additions & 0 deletions dom/abort/abort-signal-timeout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>AbortSignal.timeout frame detach</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="iframe"></iframe>
<script>
async_test(t => {
const signal = iframe.contentWindow.AbortSignal.timeout(5);
signal.onabort = t.unreached_func("abort must not fire");

iframe.remove();

t.step_timeout(() => {
assert_false(signal.aborted);
t.done();
}, 10);
}, "Signal returned by AbortSignal.timeout() is not aborted after frame detach");
</script>

0 comments on commit 2036b08

Please sign in to comment.