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: ensure dynamic event handlers are wrapped in a derived #12563

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Jul 23, 2024

Addresses the issue mentioned in #12519 that is for Svelte 5. Fixes #12520.

Copy link

changeset-bot bot commented Jul 23, 2024

🦋 Changeset detected

Latest commit: cb728f0

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

We probably only want to do this if there's a CallExpression, no? That's the standard we apply elsewhere

@Rich-Harris
Copy link
Member

i.e. here...

<script>
  let count = $state(0);

  const handlers = {
    increment: () => count += 1
  };
</script>

<button onclick={handlers.increment}>
  clicks: {count}
</button>
var button = root();
-var event_handler = $.derived(() => handlers.increment);

button.__click = function (...$$args) {
- return $.get(event_handler)?.apply(this, $$args);
+ return handlers.increment?.apply(this, $$args);
};

@trueadm
Copy link
Contributor Author

trueadm commented Jul 23, 2024

We probably only want to do this if there's a CallExpression, no? That's the standard we apply elsewhere

Ah yeah, I don't need that other change anymore. Good spot!

@Rich-Harris
Copy link
Member

We're still creating a derived for conditional/logical expressions, which we don't do in other scenarios. Worth avoiding?

@trueadm
Copy link
Contributor Author

trueadm commented Jul 23, 2024

@Rich-Harris I can split it out so we only create dynamic deriveds for call expressions, but we still need to keep dynamic handlers. I guess we would also need to be more thorough as the conditional could contain a call expression.

@Rich-Harris
Copy link
Member

We have a mechanism for this that we use everywhere else we face the same problem — contains_call_expression — which handles the 'call expression inside a conditional expression' case. Any reason we can't use it here?

@trueadm
Copy link
Contributor Author

trueadm commented Jul 23, 2024

We have a mechanism for this that we use everywhere else we face the same problem — contains_call_expression — which handles the 'call expression inside a conditional expression' case. Any reason we can't use it here?

That's what I tried originally, unfortunately, it doesn't seem like it's available? It's not on the handler node aka node.expression.

@trueadm
Copy link
Contributor Author

trueadm commented Jul 23, 2024

Ah figured it out!

@Rich-Harris Rich-Harris merged commit d17755a into main Jul 23, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the dynamic-event branch July 23, 2024 19:27
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.

Svelte 5: Higher order dynamic event handlers are not reactive
2 participants