Skip to content

fix: ensure event delegation stops after an error #11239

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

Closed
wants to merge 1 commit into from

Conversation

dummdidumm
Copy link
Member

When a delegated event handler throws an error, ensure that delegated event handlers above it don't run. Strictly speaking this is different from attaching multiple separate event listeners because those above would still run, but it's impossible for us to simulate this behavior here. Fixes #8403

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.

Tests and linting

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

When a delegated event handler throws an error, ensure that delegated event handlers above it don't run.
Strictly speaking this is different from attaching multiple separate event listeners because those above would still run, but it's impossible for us to simulate this behavior here.
Fixes #8403
Copy link

changeset-bot bot commented Apr 19, 2024

🦋 Changeset detected

Latest commit: 329e8c2

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

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

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

@Rich-Harris
Copy link
Member

Surely we want handlers higher up in the tree to run normally? Errors shouldn't stop propagation. In #8403 (comment), the weirdness I was referring to was that the 'nope' error was logged twice when you hit the first button, instead of behaving the same as the second button (notwithstanding that the second button also behaves weirdly after the first click). I'd expect both buttons to result in one 'nope' error followed by a 'hello'.

(This would technically be a breaking change since this is also broken in Svelte 4.)

@dummdidumm
Copy link
Member Author

But how do you want to do that? The traversal upwards is synchronous, and not throwing an error (instead a console.error) would not be the same, wouldn't it?

@Rich-Harris
Copy link
Member

Rich-Harris commented Apr 19, 2024

I think you can do it by recursing inside a finally block:

function nope() {
  throw new Error('nope');
}

function yep() {
  console.log('hello');
}

function go(handlers, i = 0) {
  try {
    handlers[i]();
  } finally {
    if (++i < handlers.length) {
      go(handlers, i);
    }
  }
}

go([nope, yep]);

@Rich-Harris
Copy link
Member

opened #11263 as an alternative

@Rich-Harris
Copy link
Member

merged #11263 — closing

@Conduitry Conduitry deleted the event-attribute-error-handling branch August 15, 2024 10:17
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.

Inconsistent exception handling with multiple event handlers
2 participants