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

fix: run event attributes after binding event listeners #11230

Merged
merged 3 commits into from
Apr 19, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Apr 18, 2024

By running the event listener logic inside an effect on the first run we guarantee that they're attached after binding listeners.
Fixes #11138
Fixes #11160
In contrast to #11229 this is a hilariously simple change, though it does not ensure that event attributes run after event listeners added inside action attributes. If we wanted that, we would need to return the list of events from the function, and then run them in an effect we can place at the very end of the current block. Should we that or not?

While working on this I noticed two things:

  • generated spreading code can be optimized, instead of [{ foo: .. }, rest, { bar: .. }] we should be able to do { foo: .., ...rest, bar: .. }
  • when you use lambda functions for event handlers, they'll be removed and added again as listeners on every rerun, completely unnecessary. We should detect this case and put the lambdas into variables so their identity is stable between reruns, avoiding this

Both can happen in a followup PR or as part of this one, depending on what the response here is by tomorrow. Added both these improvements.

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

Copy link

changeset-bot bot commented Apr 18, 2024

🦋 Changeset detected

Latest commit: ff13811

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

By running the event listener logic inside an effect on the first run we guarantee that they're attached after binding listeners. Fixes #11138.
Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

beautiful

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