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,7 +187,12 @@ 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?.kind !== 'derived' &&
binding?.kind !== 'raw_state' &&
!ownerships_effects.has(left?.name)
) {
ownerships_effects.set(left?.name, () => {
binding_initializers.push( binding_initializers.push(
b.stmt( b.stmt(
b.call( b.call(
@ -196,6 +203,7 @@ export function build_component(node, component_name, context, anchor = context.
) )
) )
); );
});
} }
} }
@ -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