-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
event.currentTarget
wrongly changes reference to the <body> after an await
, inside an onclick event
#11206
Comments
There's not much we can do about this. We can list this as a breaking change, but the event object is shared along the listeners to improve performance. I thought we might be able to detect an async closure and bail-out but you could pass that along anywhere with the event object. |
Yeah, I thought this was just a bug of svelte. I didn't realized it was almost standard behaviour (in firefox |
@madacol I don't know what happened to The question instead is why the Try the following code in the Svelte4 REPL and Svelte5 REPL: <button on:click={async event => {
const button = event.currentTarget;
console.log(event.currentTarget === button); // true
await new Promise((resolve) => setTimeout(resolve, 100));
console.log(event.currentTarget === button); // false
}}>Click me</button> |
Hey @Conduitry this is actually a bug?, two things I noticed after I went through the code. (For now we will just compare v4 and v5(which includes v4 behind the hood)) on:Click : I use
which means the currentTarget is button and the param
onclick: this handled entirely in a very different way i.e.,
Here two event listeners are added one in the root, which can be body and another is dom, now first listnere got called but in this case the Till now on the first check i.e., Now await is called in between 2nd listener is activated and now the Just wanted to understand, is making changes in the event object, will not create more bugs? |
@trueadm |
What other frameworks do, and what we could also offer, is a way to opt out of event delegation through a compiler option. I'm not sure that this bug right here warrants changing anything though - especially considering that other frameworks like React and Solid have the same gotcha. |
This comment was marked as outdated.
This comment was marked as outdated.
@brunnerh Event delegation has a significant impact on performance in real world applications. React has an |
For the record, these are the differences I have noticed about how and when Svelte 5
|
@madacol Yes, as I mentioned above, this is to be expected when using |
No, there is no synchronous resolves What you see is that the entire handler is delayed asynchronously. This is what What we need to know is that according to the documentation, So yes, when the |
Describe the bug
The first
event.currentTarget
correctly references thebutton
, but after awaiting the promise (or in a then() ), it changes reference to thebody
Problem disappears if I change the
onclick
to the oldon:click
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA52Qvw6CMBDGX-XSCRIDO1oS4ws4uKlDLSdpLD3SXjGE8O6WiE5OTvfvu--Xu0ncjcUgqvMknOpQVGLf92IjeOyXIgxoGVMdKHq9dHa3yEwOyGlr9ENOKoxOAw7oGGQN08VdWJMLDKtSvoeFjt6neFK-Rd5-ZGSxsNRmPzQgpVxN8i2UJazsetlVT2UYHD7h6KkzAbPMY3IbMJf1mmV5_h-GmjFB5rk-LDdCh7vyi07P6Kgxd4ONqNhHnK_zC1Gr0X5HAQAA
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: