@@ -74,6 +74,7 @@ class IfBlockBranch extends Wrapper {
74
74
export default class IfBlockWrapper extends Wrapper {
75
75
node : IfBlock ;
76
76
branches : IfBlockBranch [ ] ;
77
+ needs_update = false ;
77
78
78
79
var = 'if_block' ;
79
80
@@ -112,10 +113,16 @@ export default class IfBlockWrapper extends Wrapper {
112
113
block . add_dependencies ( node . expression . dependencies ) ;
113
114
114
115
if ( branch . block . dependencies . size > 0 ) {
116
+ // the condition, or its contents, is dynamic
115
117
is_dynamic = true ;
116
118
block . add_dependencies ( branch . block . dependencies ) ;
117
119
}
118
120
121
+ if ( branch . dependencies && branch . dependencies . length > 0 ) {
122
+ // the condition itself is dynamic
123
+ this . needs_update = true ;
124
+ }
125
+
119
126
if ( branch . block . has_intros ) has_intros = true ;
120
127
if ( branch . block . has_outros ) has_outros = true ;
121
128
@@ -239,15 +246,29 @@ export default class IfBlockWrapper extends Wrapper {
239
246
const current_block_type_and = has_else ? '' : `${ current_block_type } && ` ;
240
247
241
248
/* eslint-disable @typescript-eslint/indent,indent */
242
- block . builders . init . add_block ( deindent `
243
- function ${ select_block_type } (changed, ctx) {
244
- ${ this . branches . map ( ( { dependencies, condition, snippet, block } ) => condition
245
- ? deindent `
246
- ${ snippet && `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )` }
247
- if (${ condition } ) return ${ block . name } ;`
248
- : `return ${ block . name } ;` ) }
249
- }
250
- ` ) ;
249
+ if ( this . needs_update ) {
250
+ block . builders . init . add_block ( deindent `
251
+ function ${ select_block_type } (changed, ctx) {
252
+ ${ this . branches . map ( ( { dependencies, condition, snippet, block } ) => condition
253
+ ? deindent `
254
+ ${ snippet && (
255
+ dependencies . length > 0
256
+ ? `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )`
257
+ : `if (${ condition } == null) ${ condition } = !!(${ snippet } )`
258
+ ) }
259
+ if (${ condition } ) return ${ block . name } ;`
260
+ : `return ${ block . name } ;` ) }
261
+ }
262
+ ` ) ;
263
+ } else {
264
+ block . builders . init . add_block ( deindent `
265
+ function ${ select_block_type } (changed, ctx) {
266
+ ${ this . branches . map ( ( { condition, snippet, block } ) => condition
267
+ ? `if (${ snippet || condition } ) return ${ block . name } ;`
268
+ : `return ${ block . name } ;` ) }
269
+ }
270
+ ` ) ;
271
+ }
251
272
/* eslint-enable @typescript-eslint/indent,indent */
252
273
253
274
block . builders . init . add_block ( deindent `
@@ -261,32 +282,36 @@ export default class IfBlockWrapper extends Wrapper {
261
282
`${ if_name } ${ name } .m(${ initial_mount_node } , ${ anchor_node } );`
262
283
) ;
263
284
264
- const update_mount_node = this . get_update_mount_node ( anchor ) ;
285
+ if ( this . needs_update ) {
286
+ const update_mount_node = this . get_update_mount_node ( anchor ) ;
265
287
266
- const change_block = deindent `
267
- ${ if_name } ${ name } .d(1);
268
- ${ name } = ${ current_block_type_and } ${ current_block_type } (ctx);
269
- if (${ name } ) {
270
- ${ name } .c();
271
- ${ has_transitions && `@transition_in(${ name } , 1);` }
272
- ${ name } .m(${ update_mount_node } , ${ anchor } );
273
- }
274
- ` ;
275
-
276
- if ( dynamic ) {
277
- block . builders . update . add_block ( deindent `
278
- if (${ current_block_type } === (${ current_block_type } = ${ select_block_type } (changed, ctx)) && ${ name } ) {
279
- ${ name } .p(changed, ctx);
280
- } else {
281
- ${ change_block }
282
- }
283
- ` ) ;
284
- } else {
285
- block . builders . update . add_block ( deindent `
286
- if (${ current_block_type } !== (${ current_block_type } = ${ select_block_type } (changed, ctx))) {
287
- ${ change_block }
288
+ const change_block = deindent `
289
+ ${ if_name } ${ name } .d(1);
290
+ ${ name } = ${ current_block_type_and } ${ current_block_type } (ctx);
291
+ if (${ name } ) {
292
+ ${ name } .c();
293
+ ${ has_transitions && `@transition_in(${ name } , 1);` }
294
+ ${ name } .m(${ update_mount_node } , ${ anchor } );
288
295
}
289
- ` ) ;
296
+ ` ;
297
+
298
+ if ( dynamic ) {
299
+ block . builders . update . add_block ( deindent `
300
+ if (${ current_block_type } === (${ current_block_type } = ${ select_block_type } (changed, ctx)) && ${ name } ) {
301
+ ${ name } .p(changed, ctx);
302
+ } else {
303
+ ${ change_block }
304
+ }
305
+ ` ) ;
306
+ } else {
307
+ block . builders . update . add_block ( deindent `
308
+ if (${ current_block_type } !== (${ current_block_type } = ${ select_block_type } (changed, ctx))) {
309
+ ${ change_block }
310
+ }
311
+ ` ) ;
312
+ }
313
+ } else if ( dynamic ) {
314
+ block . builders . update . add_line ( `${ name } .p(changed, ctx);` ) ;
290
315
}
291
316
292
317
block . builders . destroy . add_line ( `${ if_name } ${ name } .d(${ detaching } );` ) ;
@@ -323,14 +348,25 @@ export default class IfBlockWrapper extends Wrapper {
323
348
324
349
var ${ if_blocks } = [];
325
350
326
- function ${ select_block_type } (changed, ctx) {
327
- ${ this . branches . map ( ( { dependencies, condition, snippet } , i ) => condition
351
+ ${ this . needs_update
328
352
? deindent `
329
- ${ snippet && `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )` }
330
- if (${ condition } ) return ${ String ( i ) } ;`
331
- : `return ${ i } ;` ) }
332
- ${ ! has_else && `return -1;` }
333
- }
353
+ function ${ select_block_type } (changed, ctx) {
354
+ ${ this . branches . map ( ( { dependencies, condition, snippet } , i ) => condition
355
+ ? deindent `
356
+ ${ snippet && `if ((${ condition } == null) || ${ dependencies . map ( n => `changed.${ n } ` ) . join ( ' || ' ) } ) ${ condition } = !!(${ snippet } )` }
357
+ if (${ condition } ) return ${ String ( i ) } ;`
358
+ : `return ${ i } ;` ) }
359
+ ${ ! has_else && `return -1;` }
360
+ }
361
+ `
362
+ : deindent `
363
+ function ${ select_block_type } (changed, ctx) {
364
+ ${ this . branches . map ( ( { condition, snippet } , i ) => condition
365
+ ? `if (${ snippet || condition } ) return ${ String ( i ) } ;`
366
+ : `return ${ i } ;` ) }
367
+ ${ ! has_else && `return -1;` }
368
+ }
369
+ ` }
334
370
` ) ;
335
371
/* eslint-enable @typescript-eslint/indent,indent */
336
372
@@ -354,62 +390,66 @@ export default class IfBlockWrapper extends Wrapper {
354
390
`${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].m(${ initial_mount_node } , ${ anchor_node } );`
355
391
) ;
356
392
357
- const update_mount_node = this . get_update_mount_node ( anchor ) ;
358
-
359
- const destroy_old_block = deindent `
360
- @group_outros();
361
- @transition_out(${ if_blocks } [${ previous_block_index } ], 1, 1, () => {
362
- ${ if_blocks } [${ previous_block_index } ] = null;
363
- });
364
- @check_outros();
365
- ` ;
393
+ if ( this . needs_update ) {
394
+ const update_mount_node = this . get_update_mount_node ( anchor ) ;
366
395
367
- const create_new_block = deindent `
368
- ${ name } = ${ if_blocks } [${ current_block_type_index } ];
369
- if (!${ name } ) {
370
- ${ name } = ${ if_blocks } [${ current_block_type_index } ] = ${ if_block_creators } [${ current_block_type_index } ](ctx);
371
- ${ name } .c();
372
- }
373
- ${ has_transitions && `@transition_in(${ name } , 1);` }
374
- ${ name } .m(${ update_mount_node } , ${ anchor } );
375
- ` ;
396
+ const destroy_old_block = deindent `
397
+ @group_outros();
398
+ @transition_out(${ if_blocks } [${ previous_block_index } ], 1, 1, () => {
399
+ ${ if_blocks } [${ previous_block_index } ] = null;
400
+ });
401
+ @check_outros();
402
+ ` ;
376
403
377
- const change_block = has_else
378
- ? deindent `
379
- ${ destroy_old_block }
404
+ const create_new_block = deindent `
405
+ ${ name } = ${ if_blocks } [${ current_block_type_index } ];
406
+ if (!${ name } ) {
407
+ ${ name } = ${ if_blocks } [${ current_block_type_index } ] = ${ if_block_creators } [${ current_block_type_index } ](ctx);
408
+ ${ name } .c();
409
+ }
410
+ ${ has_transitions && `@transition_in(${ name } , 1);` }
411
+ ${ name } .m(${ update_mount_node } , ${ anchor } );
412
+ ` ;
380
413
381
- ${ create_new_block }
382
- `
383
- : deindent `
384
- if (${ name } ) {
414
+ const change_block = has_else
415
+ ? deindent `
385
416
${ destroy_old_block }
386
- }
387
417
388
- if (~${ current_block_type_index } ) {
389
418
${ create_new_block }
390
- } else {
391
- ${ name } = null;
392
- }
393
- ` ;
419
+ `
420
+ : deindent `
421
+ if (${ name } ) {
422
+ ${ destroy_old_block }
423
+ }
394
424
395
- if ( dynamic ) {
396
- block . builders . update . add_block ( deindent `
397
- var ${ previous_block_index } = ${ current_block_type_index } ;
398
- ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
399
- if (${ current_block_type_index } === ${ previous_block_index } ) {
400
- ${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].p(changed, ctx);
401
- } else {
402
- ${ change_block }
403
- }
404
- ` ) ;
405
- } else {
406
- block . builders . update . add_block ( deindent `
407
- var ${ previous_block_index } = ${ current_block_type_index } ;
408
- ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
409
- if (${ current_block_type_index } !== ${ previous_block_index } ) {
410
- ${ change_block }
411
- }
412
- ` ) ;
425
+ if (~${ current_block_type_index } ) {
426
+ ${ create_new_block }
427
+ } else {
428
+ ${ name } = null;
429
+ }
430
+ ` ;
431
+
432
+ if ( dynamic ) {
433
+ block . builders . update . add_block ( deindent `
434
+ var ${ previous_block_index } = ${ current_block_type_index } ;
435
+ ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
436
+ if (${ current_block_type_index } === ${ previous_block_index } ) {
437
+ ${ if_current_block_type_index } ${ if_blocks } [${ current_block_type_index } ].p(changed, ctx);
438
+ } else {
439
+ ${ change_block }
440
+ }
441
+ ` ) ;
442
+ } else {
443
+ block . builders . update . add_block ( deindent `
444
+ var ${ previous_block_index } = ${ current_block_type_index } ;
445
+ ${ current_block_type_index } = ${ select_block_type } (changed, ctx);
446
+ if (${ current_block_type_index } !== ${ previous_block_index } ) {
447
+ ${ change_block }
448
+ }
449
+ ` ) ;
450
+ }
451
+ } else if ( dynamic ) {
452
+ block . builders . update . add_line ( `${ name } .p(changed, ctx);` ) ;
413
453
}
414
454
415
455
block . builders . destroy . add_line ( deindent `
0 commit comments