@@ -93,6 +93,8 @@ export function build_component(node, component_name, context, anchor = context.
9393 }
9494 }
9595
96+ const ownerships_effects = new Map ( ) ;
97+
9698 for ( const attribute of node . attributes ) {
9799 if ( attribute . type === 'LetDirective' ) {
98100 if ( ! slot_scope_applies_to_itself ) {
@@ -185,17 +187,23 @@ export function build_component(node, component_name, context, anchor = context.
185187 // Only run ownership addition on $state fields.
186188 // Theoretically someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`,
187189 // but that feels so much of an edge case that it doesn't warrant a perf hit for the common case.
188- if ( binding ?. kind !== 'derived' && binding ?. kind !== 'raw_state' ) {
189- binding_initializers . push (
190- b . stmt (
191- b . call (
192- b . id ( '$.add_owner_effect' ) ,
193- b . thunk ( expression ) ,
194- b . id ( component_name ) ,
195- is_ignored ( node , 'ownership_invalid_binding' ) && b . true
190+ if (
191+ binding ?. kind !== 'derived' &&
192+ binding ?. kind !== 'raw_state' &&
193+ ! ownerships_effects . has ( left ?. name )
194+ ) {
195+ ownerships_effects . set ( left ?. name , ( ) => {
196+ binding_initializers . push (
197+ b . stmt (
198+ b . call (
199+ b . id ( '$.add_owner_effect' ) ,
200+ b . thunk ( expression ) ,
201+ b . id ( component_name ) ,
202+ is_ignored ( node , 'ownership_invalid_binding' ) && b . true
203+ )
196204 )
197- )
198- ) ;
205+ ) ;
206+ } ) ;
199207 }
200208 }
201209
@@ -255,6 +263,10 @@ export function build_component(node, component_name, context, anchor = context.
255263 }
256264 }
257265
266+ for ( let [ , ownership_effect ] of ownerships_effects ) {
267+ ownership_effect ( ) ;
268+ }
269+
258270 delayed_props . forEach ( ( fn ) => fn ( ) ) ;
259271
260272 if ( slot_scope_applies_to_itself ) {
0 commit comments