Skip to content
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

Ability to reset an AbortSignal.timeout #1263

Open
jasnell opened this issue Mar 2, 2024 · 3 comments
Open

Ability to reset an AbortSignal.timeout #1263

jasnell opened this issue Mar 2, 2024 · 3 comments

Comments

@jasnell
Copy link
Contributor

jasnell commented Mar 2, 2024

What is the issue with the DOM Standard?

The AbortSignal.timeout(...) method provided us with a nice functional mechanism to abort an operation after a given period of time and was quite a welcome addition. However, there is one use case that it currently does not quite adequately cover: idle timeouts. That is, given a long running operation, the ability to timeout only if there is not regular activity within a given period of time. If there is activity, the timeout resets.

For example, let's imagine that we have an async iterable like a ReadableStream. Once we start consuming the stream, we want to make sure that each read happens within about one second and that the stream is canceled if the reads do not occur fast enough. This would be difficult with AbortSignal.timeout(...) currently without being forced to create a new AbortSignal on every iteration, which seems wasteful. Instead, it would be nice to have a single AbortSignal timeout that can be reset.

const signal = AbortSignal.timeout(1000);

// do some work...

signal.update();  // reset the timer...

// do more work...

signal.update();  // reset the timer...

Obviously, however, this becomes a bit weird to have an update() method on AbortSignal instances created in other ways that does not do anything, so I'm not that worried about exactly what the api looks like... I'd mostly like to explore if this use case (specifically, allowing a single timeout-based AbortSignal to have it's timer reset) would be interesting.

@domenic
Copy link
Member

domenic commented Mar 2, 2024

Just using setTimeout + AbortController manually for this case seems fine?

@jasnell
Copy link
Contributor Author

jasnell commented Mar 3, 2024

Perhaps, and is how I've addressed this previously, it just feels a bit... awkward. I mean, that same thing could have been said about AbortSignal.timeout() as well (and probably was, to be fair).

@annevk
Copy link
Member

annevk commented Mar 3, 2024

fetch() has some more detailed timer tracking issues for cases like this: whatwg/fetch#179 & whatwg/fetch#180. Perhaps they ought to be options for streams instead, interesting thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants