Skip to content

Commit f6eba05

Browse files
committed
extend fix to blocks with outros
1 parent 9c9f37c commit f6eba05

File tree

5 files changed

+112
-56
lines changed

5 files changed

+112
-56
lines changed

src/compiler/compile/render_dom/wrappers/IfBlock.ts

+97-56
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,32 @@ export default class IfBlockWrapper extends Wrapper {
340340
block.add_variable(current_block_type_index);
341341
block.add_variable(name);
342342

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+
343369
/* eslint-disable @typescript-eslint/indent,indent */
344370
block.builders.init.add_block(deindent`
345371
var ${if_block_creators} = [
@@ -348,14 +374,25 @@ export default class IfBlockWrapper extends Wrapper {
348374
349375
var ${if_blocks} = [];
350376
351-
function ${select_block_type}(changed, ctx) {
352-
${this.branches.map(({ dependencies, condition, snippet }, i) => condition
377+
${this.needs_update
353378
? 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+
`}
359396
`);
360397
/* eslint-enable @typescript-eslint/indent,indent */
361398

@@ -379,62 +416,66 @@ export default class IfBlockWrapper extends Wrapper {
379416
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].m(${initial_mount_node}, ${anchor_node});`
380417
);
381418

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);
391421

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+
`;
401429

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+
`;
405439

406-
${create_new_block}
407-
`
408-
: deindent`
409-
if (${name}) {
440+
const change_block = has_else
441+
? deindent`
410442
${destroy_old_block}
411-
}
412443
413-
if (~${current_block_type_index}) {
414444
${create_new_block}
415-
} else {
416-
${name} = null;
417-
}
418-
`;
445+
`
446+
: deindent`
447+
if (${name}) {
448+
${destroy_old_block}
449+
}
419450
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);`);
438479
}
439480

440481
block.builders.destroy.add_line(deindent`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eee
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rrr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: 'eee'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import EEE from './EEE.svelte';
3+
import RRR from './RRR.svelte';
4+
</script>
5+
6+
{#if "Eva".startsWith('E')}
7+
<EEE/>
8+
{:else}
9+
<RRR/>
10+
{/if}

0 commit comments

Comments
 (0)