@@ -340,6 +340,32 @@ export default class IfBlockWrapper extends Wrapper {
340
340
block . add_variable ( current_block_type_index ) ;
341
341
block . add_variable ( name ) ;
342
342
343
+ /* eslint-disable @typescript-eslint/indent,indent */
344
+ if ( this . needs_update ) {
345
+ block . builders . init . add_block ( deindent `
346
+ function ${ select_block_type } (changed, ctx) {
347
+ ${ this . branches . map ( ( { dependencies, condition, snippet, block } ) => condition
348
+ ? deindent `
349
+ ${ snippet && (
350
+ dependencies . length > 0
351
+ ? `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )`
352
+ : `if (${ condition } == null) ${ condition } = !!(${ snippet } )`
353
+ ) }
354
+ if (${ condition } ) return ${ block . name } ;`
355
+ : `return ${ block . name } ;` ) }
356
+ }
357
+ ` ) ;
358
+ } else {
359
+ block . builders . init . add_block ( deindent `
360
+ function ${ select_block_type } (changed, ctx) {
361
+ ${ this . branches . map ( ( { condition, snippet, block } ) => condition
362
+ ? `if (${ snippet } ) return ${ block . name } ;`
363
+ : `return ${ block . name } ;` ) }
364
+ }
365
+ ` ) ;
366
+ }
367
+ /* eslint-enable @typescript-eslint/indent,indent */
368
+
343
369
/* eslint-disable @typescript-eslint/indent,indent */
344
370
block . builders . init . add_block ( deindent `
345
371
var ${ if_block_creators } = [
@@ -348,14 +374,25 @@ export default class IfBlockWrapper extends Wrapper {
348
374
349
375
var ${ if_blocks } = [];
350
376
351
- function ${ select_block_type } (changed, ctx) {
352
- ${ this . branches . map ( ( { dependencies, condition, snippet } , i ) => condition
377
+ ${ this . needs_update
353
378
? deindent `
354
- ${ snippet && `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )` }
355
- if (${ condition } ) return ${ String ( i ) } ;`
356
- : `return ${ i } ;` ) }
357
- ${ ! has_else && `return -1;` }
358
- }
379
+ function ${ select_block_type } (changed, ctx) {
380
+ ${ this . branches . map ( ( { dependencies, condition, snippet } , i ) => condition
381
+ ? deindent `
382
+ ${ snippet && `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )` }
383
+ if (${ condition } ) return ${ String ( i ) } ;`
384
+ : `return ${ i } ;` ) }
385
+ ${ ! has_else && `return -1;` }
386
+ }
387
+ `
388
+ : deindent `
389
+ function ${ select_block_type } (changed, ctx) {
390
+ ${ this . branches . map ( ( { condition, snippet } , i ) => condition
391
+ ? `if (${ snippet } ) return ${ String ( i ) } ;`
392
+ : `return ${ i } ;` ) }
393
+ ${ ! has_else && `return -1;` }
394
+ }
395
+ ` }
359
396
` ) ;
360
397
/* eslint-enable @typescript-eslint/indent,indent */
361
398
@@ -379,62 +416,66 @@ export default class IfBlockWrapper extends Wrapper {
379
416
`${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].m(${ initial_mount_node } , ${ anchor_node } );`
380
417
) ;
381
418
382
- const update_mount_node = this . get_update_mount_node ( anchor ) ;
383
-
384
- const destroy_old_block = deindent `
385
- @group_outros();
386
- @transition_out(${ if_blocks } [${ previous_block_index } ], 1, 1, () => {
387
- ${ if_blocks } [${ previous_block_index } ] = null;
388
- });
389
- @check_outros();
390
- ` ;
419
+ if ( this . needs_update ) {
420
+ const update_mount_node = this . get_update_mount_node ( anchor ) ;
391
421
392
- const create_new_block = deindent `
393
- ${ name } = ${ if_blocks } [${ current_block_type_index } ];
394
- if (!${ name } ) {
395
- ${ name } = ${ if_blocks } [${ current_block_type_index } ] = ${ if_block_creators } [${ current_block_type_index } ](ctx);
396
- ${ name } .c();
397
- }
398
- ${ has_transitions && `@transition_in(${ name } , 1);` }
399
- ${ name } .m(${ update_mount_node } , ${ anchor } );
400
- ` ;
422
+ const destroy_old_block = deindent `
423
+ @group_outros();
424
+ @transition_out(${ if_blocks } [${ previous_block_index } ], 1, 1, () => {
425
+ ${ if_blocks } [${ previous_block_index } ] = null;
426
+ });
427
+ @check_outros();
428
+ ` ;
401
429
402
- const change_block = has_else
403
- ? deindent `
404
- ${ destroy_old_block }
430
+ const create_new_block = deindent `
431
+ ${ name } = ${ if_blocks } [${ current_block_type_index } ];
432
+ if (!${ name } ) {
433
+ ${ name } = ${ if_blocks } [${ current_block_type_index } ] = ${ if_block_creators } [${ current_block_type_index } ](ctx);
434
+ ${ name } .c();
435
+ }
436
+ ${ has_transitions && `@transition_in(${ name } , 1);` }
437
+ ${ name } .m(${ update_mount_node } , ${ anchor } );
438
+ ` ;
405
439
406
- ${ create_new_block }
407
- `
408
- : deindent `
409
- if (${ name } ) {
440
+ const change_block = has_else
441
+ ? deindent `
410
442
${ destroy_old_block }
411
- }
412
443
413
- if (~${ current_block_type_index } ) {
414
444
${ create_new_block }
415
- } else {
416
- ${ name } = null;
417
- }
418
- ` ;
445
+ `
446
+ : deindent `
447
+ if (${ name } ) {
448
+ ${ destroy_old_block }
449
+ }
419
450
420
- if ( dynamic ) {
421
- block . builders . update . add_block ( deindent `
422
- var ${ previous_block_index } = ${ current_block_type_index } ;
423
- ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
424
- if (${ current_block_type_index } === ${ previous_block_index } ) {
425
- ${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].p(changed, ctx);
426
- } else {
427
- ${ change_block }
428
- }
429
- ` ) ;
430
- } else {
431
- block . builders . update . add_block ( deindent `
432
- var ${ previous_block_index } = ${ current_block_type_index } ;
433
- ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
434
- if (${ current_block_type_index } !== ${ previous_block_index } ) {
435
- ${ change_block }
436
- }
437
- ` ) ;
451
+ if (~${ current_block_type_index } ) {
452
+ ${ create_new_block }
453
+ } else {
454
+ ${ name } = null;
455
+ }
456
+ ` ;
457
+
458
+ if ( dynamic ) {
459
+ block . builders . update . add_block ( deindent `
460
+ var ${ previous_block_index } = ${ current_block_type_index } ;
461
+ ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
462
+ if (${ current_block_type_index } === ${ previous_block_index } ) {
463
+ ${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].p(changed, ctx);
464
+ } else {
465
+ ${ change_block }
466
+ }
467
+ ` ) ;
468
+ } else {
469
+ block . builders . update . add_block ( deindent `
470
+ var ${ previous_block_index } = ${ current_block_type_index } ;
471
+ ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
472
+ if (${ current_block_type_index } !== ${ previous_block_index } ) {
473
+ ${ change_block }
474
+ }
475
+ ` ) ;
476
+ }
477
+ } else if ( dynamic ) {
478
+ block . builders . update . add_line ( `${ name } .p(changed, ctx);` ) ;
438
479
}
439
480
440
481
block . builders . destroy . add_line ( deindent `
0 commit comments