extend fix to blocks with outros

pull/3518/head
Rich Harris 5 years ago
parent 9c9f37c001
commit f6eba05f73

@ -340,6 +340,32 @@ export default class IfBlockWrapper extends Wrapper {
block.add_variable(current_block_type_index);
block.add_variable(name);
/* eslint-disable @typescript-eslint/indent,indent */
if (this.needs_update) {
block.builders.init.add_block(deindent`
function ${select_block_type}(changed, ctx) {
${this.branches.map(({ dependencies, condition, snippet, block }) => condition
? deindent`
${snippet && (
dependencies.length > 0
? `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`
: `if (${condition} == null) ${condition} = !!(${snippet})`
)}
if (${condition}) return ${block.name};`
: `return ${block.name};`)}
}
`);
} else {
block.builders.init.add_block(deindent`
function ${select_block_type}(changed, ctx) {
${this.branches.map(({ condition, snippet, block }) => condition
? `if (${snippet}) return ${block.name};`
: `return ${block.name};`)}
}
`);
}
/* eslint-enable @typescript-eslint/indent,indent */
/* eslint-disable @typescript-eslint/indent,indent */
block.builders.init.add_block(deindent`
var ${if_block_creators} = [
@ -348,6 +374,8 @@ export default class IfBlockWrapper extends Wrapper {
var ${if_blocks} = [];
${this.needs_update
? deindent`
function ${select_block_type}(changed, ctx) {
${this.branches.map(({ dependencies, condition, snippet }, i) => condition
? deindent`
@ -356,6 +384,15 @@ export default class IfBlockWrapper extends Wrapper {
: `return ${i};`)}
${!has_else && `return -1;`}
}
`
: deindent`
function ${select_block_type}(changed, ctx) {
${this.branches.map(({ condition, snippet }, i) => condition
? `if (${snippet}) return ${String(i)};`
: `return ${i};`)}
${!has_else && `return -1;`}
}
`}
`);
/* eslint-enable @typescript-eslint/indent,indent */
@ -379,6 +416,7 @@ export default class IfBlockWrapper extends Wrapper {
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].m(${initial_mount_node}, ${anchor_node});`
);
if (this.needs_update) {
const update_mount_node = this.get_update_mount_node(anchor);
const destroy_old_block = deindent`
@ -436,6 +474,9 @@ export default class IfBlockWrapper extends Wrapper {
}
`);
}
} else if (dynamic) {
block.builders.update.add_line(`${name}.p(changed, ctx);`);
}
block.builders.destroy.add_line(deindent`
${if_current_block_type_index}${if_blocks}[${current_block_type_index}].d(${detaching});

@ -0,0 +1,10 @@
<script>
import EEE from './EEE.svelte';
import RRR from './RRR.svelte';
</script>
{#if "Eva".startsWith('E')}
<EEE/>
{:else}
<RRR/>
{/if}
Loading…
Cancel
Save