feat: add `getAbortSignal()` by Rich-Harris · Pull Request #16266 · sveltejs/svelte · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

feat: add getAbortSignal() #16266

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

Merged
merged 14 commits into from
Jun 30, 2025
Merged

feat: add getAbortSignal() #16266

merged 14 commits into from
Jun 30, 2025

Conversation

Rich-Harris
Copy link
Member

This extracts getAbortSignal() from #15844, since it is (at least theoretically) independently useful and is easier to review in isolation.

It may prove necessary to expose STALE_REACTION in some form, so that people can do signal.reason === ??? inside a .catch handler to determine whether or not to rethrow an error. But rather than get bogged down in design questions around what it should be called and where it should be exported from, I think we can safely cross that bridge later if we need to.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 30, 2025

🦋 Changeset detected

Latest commit: 1b5857f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16266

@Ocean-OS
Copy link
Contributor

what if instead of aborting and passing a symbol, we passed an object with { stale: boolean }?

@Rich-Harris
Copy link
Member Author

Ah so you'd do something like this?

promise.catch((error) => {
  if (error.stale) return;
  throw error;
});

That could be nice and simple.

Separately: not sure why the tests are failing in CI but not locally. Guess I'll try bumping the timeout

@@ -27,6 +27,9 @@ export const LEGACY_PROPS = Symbol('legacy props');
export const LOADING_ATTR_SYMBOL = Symbol('');
export const PROXY_PATH_SYMBOL = Symbol('proxy path');

// allow users to ignore aborted signal errors if `reason.stale`
export const STALE_REACTION = { stale: true };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this but I think it needs some refinement... right now refined users of the best typed systems would have to do something like this:

promise.catch(e => {
  if (typeof e === 'object' && Object.hasOwn(e, 'stale') && e.stale === true) {
    // stuff
  }
})

We either need to export an isStaleReactionAbortError helper that does this and types it correctly or change tact. I would also think about making this a class extending Error as right now it would fail an if (e instanceof Error) check which is always a bad feeling when catching an error...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. this is exactly the rabbit hole I wanted to avoid. my hunch/hope is that there will be vanishingly few situations where you actually need to do this check, and so my thinking was that we can punt on this for now (while adhering to reason.stale being true if we do make it an error in future)

just don't want #15844 to be held up by unnecessary bikeshedding

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine punting on any type-related stuff but changing this to an error instance seems kinda essential, throwing non-Errors in any context where you expect users to catch them seems... bad

@@ -27,6 +27,12 @@ export const LEGACY_PROPS = Symbol('legacy props');
export const LOADING_ATTR_SYMBOL = Symbol('');
export const PROXY_PATH_SYMBOL = Symbol('proxy path');

// allow users to ignore aborted signal errors if `reason.stale`
export const STALE_REACTION = new (class StaleReactionError extends Error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nasty. effective. love it

Comment on lines 280 to 283
if (reaction.ac !== null) {
reaction.ac?.abort(STALE_REACTION);
reaction.ac = null;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (reaction.ac !== null) {
reaction.ac?.abort(STALE_REACTION);
reaction.ac = null;
}
reaction.ac?.abort(STALE_REACTION);
reaction.ac = null;

I don't think the check is buying us anything here right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's saving us from writing reaction.ac, which is more expensive than reading it. though it doesn't need to be optional

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a couple of nits but lgtm now

Rich-Harris and others added 2 commits June 30, 2025 15:25
Co-authored-by: Elliott Johnson <sejohnson@torchcloudconsulting.com>
@Rich-Harris Rich-Harris merged commit b673145 into main Jun 30, 2025
7 of 14 checks passed
@Rich-Harris Rich-Harris deleted the get-abort-signal branch June 30, 2025 19:47
@github-actions github-actions bot mentioned this pull request Jul 1, 2025
@gyzerok
Copy link
Contributor

gyzerok commented Jul 2, 2025

Thats an amazing feature, great work! We will put it to a great use in our project.

Another interesting possbile use is to pass a single AbortController to all the event subscriptions, to be able to cancel them all on component destraction.

node.addEventListener('something', { signal: getAbortSignal() })

@webJose
Copy link
Contributor

webJose commented Jul 2, 2025

Can't find the docs of getAbortSignal @ https://svelte.dev, or at least the search doesn't pick up on it.

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Jul 2, 2025

Just merged a PR to update the docs, so it should be updated shortly.

@Thiagolino8
Copy link

Just merged a PR to update the docs, so it should be updated shortly.

The getAbortSignal docs have code for async svelte
Which is, at the moment, invalid

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

Successfully merging this pull request may close these issues.

6 participants

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.