From e97f40ccff2cfb78840516ef40c7816f0a1ac19f Mon Sep 17 00:00:00 2001 From: Scott Haseley Date: Wed, 2 Feb 2022 13:37:04 -0800 Subject: [PATCH] Implement AbortSignal.timeout 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: https://github.com/whatwg/dom/pull/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 Reviewed-by: Mason Freed Commit-Queue: Scott Haseley Cr-Commit-Position: refs/heads/main@{#966406} --- dom/abort/AbortSignal.any.js | 28 ++++++++++++++++++++++++++++ dom/abort/abort-signal-timeout.html | 19 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 dom/abort/abort-signal-timeout.html diff --git a/dom/abort/AbortSignal.any.js b/dom/abort/AbortSignal.any.js index 1d7d7678eb1a6e2..3bbdc11a92f90d4 100644 --- a/dom/abort/AbortSignal.any.js +++ b/dom/abort/AbortSignal.any.js @@ -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"); diff --git a/dom/abort/abort-signal-timeout.html b/dom/abort/abort-signal-timeout.html new file mode 100644 index 000000000000000..2a9c13d61434b43 --- /dev/null +++ b/dom/abort/abort-signal-timeout.html @@ -0,0 +1,19 @@ + + +AbortSignal.timeout frame detach + + + +