@@ -322,7 +322,7 @@ function serialize_get_binding(node, state) {
322
322
const store_id = b . id ( node . name . slice ( 1 ) ) ;
323
323
return b . call (
324
324
'$.store_get' ,
325
- b . id ( '$$store_subs' ) ,
325
+ b . assignment ( '??=' , b . id ( '$$store_subs' ) , b . object ( [ ] ) ) ,
326
326
b . literal ( node . name ) ,
327
327
serialize_get_binding ( store_id , state )
328
328
) ;
@@ -460,7 +460,7 @@ function serialize_set_binding(node, context, fallback) {
460
460
} else if ( is_store ) {
461
461
return b . call (
462
462
'$.mutate_store' ,
463
- b . id ( '$$store_subs' ) ,
463
+ b . assignment ( '??=' , b . id ( '$$store_subs' ) , b . object ( [ ] ) ) ,
464
464
b . literal ( left . name ) ,
465
465
b . id ( left_name ) ,
466
466
b . assignment ( node . operator , /** @type {import('estree').Pattern } */ ( visit ( node . left ) ) , value )
@@ -506,7 +506,11 @@ const global_visitors = {
506
506
if ( node . prefix ) fn += '_pre' ;
507
507
508
508
/** @type {import('estree').Expression[] } */
509
- const args = [ b . id ( '$$store_subs' ) , b . literal ( argument . name ) , b . id ( argument . name . slice ( 1 ) ) ] ;
509
+ const args = [
510
+ b . assignment ( '??=' , b . id ( '$$store_subs' ) , b . object ( [ ] ) ) ,
511
+ b . literal ( argument . name ) ,
512
+ b . id ( argument . name . slice ( 1 ) )
513
+ ] ;
510
514
if ( node . operator === '--' ) {
511
515
args . push ( b . literal ( - 1 ) ) ;
512
516
}
@@ -2094,8 +2098,10 @@ export function server_component(analysis, options) {
2094
2098
( binding ) => binding . kind === 'store_sub'
2095
2099
)
2096
2100
) {
2097
- instance . body . unshift ( b . const ( '$$store_subs' , b . object ( [ ] ) ) ) ;
2098
- template . body . push ( b . stmt ( b . call ( '$.unsubscribe_stores' , b . id ( '$$store_subs' ) ) ) ) ;
2101
+ instance . body . unshift ( b . var ( '$$store_subs' ) ) ;
2102
+ template . body . push (
2103
+ b . if ( b . id ( '$$store_subs' ) , b . stmt ( b . call ( '$.unsubscribe_stores' , b . id ( '$$store_subs' ) ) ) )
2104
+ ) ;
2099
2105
}
2100
2106
// Propagate values of bound props upwards if they're undefined in the parent and have a value.
2101
2107
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
0 commit comments