-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AbortController.prototype.timeout and AbortController.prototype.cancel #1039
Comments
If you can invoke that method multiple times, do we need |
I think |
I agree about the The proposed class TimeoutAbortController extends AbortController {
#timer;
timeout(delay) {
if (this.#timer) {
this.#timer.close(); // assuming this also cleans up the followed AbortSignal.timeout() signal
}
this.#timer = new AbortController();
this.#timer.follow(AbortSignal.timeout(delay));
this.follow(this.#timer.signal);
}
} |
And I could get on board with class TimeoutAbortController extends AbortController {
#timer;
static #abort() { this.abort(this.#timer.reason); }
timeout(delay) {
this.#timer = AbortSignal.timeout(delay);
this.#timer.addEventListener('abort', this.#abort, { once: true });
}
close() {
this.#timer?.removeEventListener('abort', this.#abort);
this.#timer = undefined;
super.close();
}
}
|
Fair point, Nit-pick: you'll still want to remove the event listener from the old
|
We'd still want the |
I've opened a PR for I'll table the proposal for |
I think close()/closed still raises some tricky questions.
|
It's possible to not have Unfortunately, we're not even doing that in the specifications. See for example:
The only spec I found that does this properly is Streams, where Maybe we should first fix how abort signals are used inside other specifications, and that might eliminate the need for a dedicated |
For an example use case from Cloudflare Workers... First, some background: The fetch When the In the Workers implementation of fetch, we are able to use the fact that we know that certain The tl;dr is -- It's helpful to know if I actually need to pay attention to the Whether that's enough of a justification is up for debate :-)
It's not entirely that simple. Many uses simply can't remove their abort algorithms if they no longer have a handle to the algorithm itself. For instance, passing in an anonymous handler
I think so. Having the |
It sounds like it might be easier to allow "this's signal" to be
Indeed, you'd have to store a reference to the added abort algorithm somewhere, probably in an internal slot. But that's also the case for "regular" event listeners, and developers already know how to do that. |
That would work in theory but given that it's currently always expected, changing that to |
Riiiiight, a request's signal is exposed as |
I opened another issue a while ago to allow attaching trusted-event only listeners which would essentially allow for the lifetime of listeners to be tied to the controller, NOT to the event-target itself. i.e. We could do: let controller = new AbortController();
controller.signal.addEventListener(
"abort",
() => { console.log("Aborted!"); },
{ trustedOnly: true },
); and if the controller performs Late listeners similarly could just be immediately discarded as they will never be callable if the controller has been collected or already aborted. The opt-in of the flag is the main thing I don't like about the idea, but it could be possible for certain events to have |
As a complement to the new
AbortSignal.timeout()
, there are a couple more cases that may be worthwhile exploring, specifically withAbortController
:abortController.timeout(delay[, reason])
AbortController
to trigger theAbortSignal
withreason
afterdelay
milliseconds.timeout()
is called, it is reset.abortController.abort()
is directly called while there is an active internal timer, the signal is immediately triggered and the internal timer is cleared.abortController.cancel()
AbortController
is no longer expected to trigger theAbortSignal
at all, allowing the AbortSignal to clear it's'abort'
algorithms and indicating that it is expected to never trigger. This can be used as a signal that consuming code can safely ignore the signal after it becomes abandoned.abortController.timeout()
, the internal timer is cleared.Example:
The text was updated successfully, but these errors were encountered: