Skip to content

fix: improve dynamic event handling #12570

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

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1148,25 +1148,7 @@ function serialize_event_handler(node, metadata, { state, visit }) {
])
);

if (handler.type === 'Identifier' || handler.type === 'MemberExpression') {
const id = object(handler);
const binding = id === null ? null : state.scope.get(id.name);
if (
binding !== null &&
(binding.kind === 'state' ||
binding.kind === 'frozen_state' ||
binding.declaration_kind === 'import' ||
binding.kind === 'legacy_reactive' ||
binding.kind === 'derived' ||
binding.kind === 'prop' ||
binding.kind === 'bindable_prop' ||
binding.kind === 'store_sub')
) {
handler = dynamic_handler();
} else {
handler = /** @type {Expression} */ (visit(handler));
}
} else if (
if (
metadata?.contains_call_expression &&
!(
(handler.type === 'ArrowFunctionExpression' || handler.type === 'FunctionExpression') &&
Expand All @@ -1193,6 +1175,24 @@ function serialize_event_handler(node, metadata, { state, visit }) {
)
])
);
} else if (handler.type === 'Identifier' || handler.type === 'MemberExpression') {
const id = object(handler);
const binding = id === null ? null : state.scope.get(id.name);
if (
binding !== null &&
(binding.kind === 'state' ||
binding.kind === 'frozen_state' ||
binding.declaration_kind === 'import' ||
binding.kind === 'legacy_reactive' ||
binding.kind === 'derived' ||
binding.kind === 'prop' ||
binding.kind === 'bindable_prop' ||
binding.kind === 'store_sub')
) {
handler = dynamic_handler();
} else {
handler = /** @type {Expression} */ (visit(handler));
}
} else if (handler.type === 'ConditionalExpression' || handler.type === 'LogicalExpression') {
handler = dynamic_handler();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test } from '../../test';

export default test({
html: '<button>Tama</button><button>Pochi</button><br><button>Change Function</button>',

test({ assert, logs, target }) {
const [b1, b2, b3] = target.querySelectorAll('button');

b1?.click();
b2?.click();
b3?.click();
b1?.click();
b2?.click();

assert.deepEqual(logs, [
'creating "Hello" handler for Tama',
'Hello Tama',
'creating "Hello" handler for Pochi',
'Hello Pochi',
'creating "Bye" handler for Tama',
'Bye Tama',
'creating "Bye" handler for Pochi',
'Bye Pochi'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
let saySomething = $state(name => {
console.log('creating "Hello" handler for ' + name);
return { handler: () => console.log('Hello ' + name) };
});

function change() {
saySomething = name => {
console.log('creating "Bye" handler for ' + name);
return { handler: () => console.log('Bye ' + name) };
}
}
</script>

<button onclick={saySomething('Tama').handler}>Tama</button>
<button onclick={saySomething('Pochi').handler}>Pochi</button>

<br>
<button onclick={change}>Change Function</button>
Loading