Skip to content

Commit 90d6f57

Browse files
authored
fix: dynamic event delegation for stateful call expressions (#12549)
1 parent 55400fd commit 90d6f57

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

.changeset/chilled-ladybugs-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: dynamic event delegation for stateful call expressions

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,11 @@ function serialize_event_handler(node, { state, visit }) {
11691169
} else {
11701170
handler = /** @type {Expression} */ (visit(handler));
11711171
}
1172-
} else if (handler.type === 'ConditionalExpression' || handler.type === 'LogicalExpression') {
1172+
} else if (
1173+
handler.type === 'CallExpression' ||
1174+
handler.type === 'ConditionalExpression' ||
1175+
handler.type === 'LogicalExpression'
1176+
) {
11731177
handler = dynamic_handler();
11741178
} else {
11751179
handler = /** @type {Expression} */ (visit(handler));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { flushSync } from 'svelte';
2+
import { test, ok } from '../../test';
3+
4+
export default test({
5+
mode: ['client'],
6+
async test({ assert, target, logs }) {
7+
const [btn1, btn2, btn3] = target.querySelectorAll('button');
8+
9+
flushSync(() => {
10+
btn1.click();
11+
btn2.click();
12+
});
13+
14+
assert.deepEqual(logs, ['AA', 'AB']);
15+
16+
flushSync(() => {
17+
btn3.click();
18+
btn1.click();
19+
btn2.click();
20+
});
21+
22+
assert.deepEqual(logs, ['AA', 'AB', 'BA', 'BB']);
23+
}
24+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let hof = $state((name) => () => console.log('A' + name));
3+
const member = $derived({
4+
hof
5+
});
6+
function change() {
7+
hof = (name) => () => console.log('B' + name);
8+
}
9+
</script>
10+
11+
<button onclick={hof('A')}>A</button>
12+
<button onclick={member.hof('B')}>B</button>
13+
14+
<br />
15+
<button onclick={change}>change</button>

0 commit comments

Comments
 (0)