@@ -263,17 +263,28 @@ function serialize_element_spread_attributes(
263
263
) {
264
264
let needs_isolation = false ;
265
265
266
- /** @type {import('estree').Expression[ ] } */
266
+ /** @type {import('estree').ObjectExpression['properties' ] } */
267
267
const values = [ ] ;
268
268
269
269
for ( const attribute of attributes ) {
270
270
if ( attribute . type === 'Attribute' ) {
271
271
const name = get_attribute_name ( element , attribute , context ) ;
272
272
// TODO: handle contains_call_expression
273
273
const [ , value ] = serialize_attribute_value ( attribute . value , context ) ;
274
- values . push ( b . object ( [ b . init ( name , value ) ] ) ) ;
274
+
275
+ if (
276
+ is_event_attribute ( attribute ) &&
277
+ attribute . value [ 0 ] . expression . type === 'ArrowFunctionExpression'
278
+ ) {
279
+ // Give the event handler a stable ID so it isn't removed and readded on every update
280
+ const id = context . state . scope . generate ( 'event_handler' ) ;
281
+ context . state . init . push ( b . var ( id , value ) ) ;
282
+ values . push ( b . init ( attribute . name , b . id ( id ) ) ) ;
283
+ } else {
284
+ values . push ( b . init ( name , value ) ) ;
285
+ }
275
286
} else {
276
- values . push ( /** @type {import('estree').Expression } */ ( context . visit ( attribute ) ) ) ;
287
+ values . push ( b . spread ( /** @type {import('estree').Expression } */ ( context . visit ( attribute ) ) ) ) ;
277
288
}
278
289
279
290
needs_isolation ||=
@@ -292,7 +303,7 @@ function serialize_element_spread_attributes(
292
303
'$.set_attributes' ,
293
304
element_id ,
294
305
b . id ( id ) ,
295
- b . array ( values ) ,
306
+ b . object ( values ) ,
296
307
lowercase_attributes ,
297
308
b . literal ( context . state . analysis . css . hash )
298
309
)
@@ -350,15 +361,26 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
350
361
let needs_isolation = false ;
351
362
let is_reactive = false ;
352
363
353
- /** @type {import('estree').Expression[ ] } */
364
+ /** @type {import('estree').ObjectExpression['properties' ] } */
354
365
const values = [ ] ;
355
366
356
367
for ( const attribute of attributes ) {
357
368
if ( attribute . type === 'Attribute' ) {
358
369
const [ , value ] = serialize_attribute_value ( attribute . value , context ) ;
359
- values . push ( b . object ( [ b . init ( attribute . name , value ) ] ) ) ;
370
+
371
+ if (
372
+ is_event_attribute ( attribute ) &&
373
+ attribute . value [ 0 ] . expression . type === 'ArrowFunctionExpression'
374
+ ) {
375
+ // Give the event handler a stable ID so it isn't removed and readded on every update
376
+ const id = context . state . scope . generate ( 'event_handler' ) ;
377
+ context . state . init . push ( b . var ( id , value ) ) ;
378
+ values . push ( b . init ( attribute . name , b . id ( id ) ) ) ;
379
+ } else {
380
+ values . push ( b . init ( attribute . name , value ) ) ;
381
+ }
360
382
} else {
361
- values . push ( /** @type {import('estree').Expression } */ ( context . visit ( attribute ) ) ) ;
383
+ values . push ( b . spread ( /** @type {import('estree').Expression } */ ( context . visit ( attribute ) ) ) ) ;
362
384
}
363
385
364
386
is_reactive ||=
@@ -381,7 +403,7 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
381
403
'$.set_dynamic_element_attributes' ,
382
404
element_id ,
383
405
b . id ( id ) ,
384
- b . array ( values ) ,
406
+ b . object ( values ) ,
385
407
b . literal ( context . state . analysis . css . hash )
386
408
)
387
409
)
@@ -402,7 +424,7 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
402
424
'$.set_dynamic_element_attributes' ,
403
425
element_id ,
404
426
b . literal ( null ) ,
405
- b . array ( values ) ,
427
+ b . object ( values ) ,
406
428
b . literal ( context . state . analysis . css . hash )
407
429
)
408
430
)
0 commit comments