chore: prevent duplicate `add_owner_effect`

ownership-function-bindings
paoloricciuti 8 months ago
parent bf80c10625
commit a00170d569

@ -93,6 +93,8 @@ export function build_component(node, component_name, context, anchor = context.
} }
} }
const ownerships_effects = new Map();
for (const attribute of node.attributes) { for (const attribute of node.attributes) {
if (attribute.type === 'LetDirective') { if (attribute.type === 'LetDirective') {
if (!slot_scope_applies_to_itself) { if (!slot_scope_applies_to_itself) {
@ -185,17 +187,23 @@ export function build_component(node, component_name, context, anchor = context.
// Only run ownership addition on $state fields. // Only run ownership addition on $state fields.
// Theoretically someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`, // Theoretically someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`,
// but that feels so much of an edge case that it doesn't warrant a perf hit for the common case. // but that feels so much of an edge case that it doesn't warrant a perf hit for the common case.
if (binding?.kind !== 'derived' && binding?.kind !== 'raw_state') { if (
binding_initializers.push( binding?.kind !== 'derived' &&
b.stmt( binding?.kind !== 'raw_state' &&
b.call( !ownerships_effects.has(left?.name)
b.id('$.add_owner_effect'), ) {
b.thunk(expression), ownerships_effects.set(left?.name, () => {
b.id(component_name), binding_initializers.push(
is_ignored(node, 'ownership_invalid_binding') && b.true b.stmt(
b.call(
b.id('$.add_owner_effect'),
b.thunk(expression),
b.id(component_name),
is_ignored(node, 'ownership_invalid_binding') && b.true
)
) )
) );
); });
} }
} }
@ -255,6 +263,10 @@ export function build_component(node, component_name, context, anchor = context.
} }
} }
for (let [, ownership_effect] of ownerships_effects) {
ownership_effect();
}
delayed_props.forEach((fn) => fn()); delayed_props.forEach((fn) => fn());
if (slot_scope_applies_to_itself) { if (slot_scope_applies_to_itself) {

Loading…
Cancel
Save