simplify if-block switching code

pull/490/head
Rich-Harris 8 years ago
parent b7a40879a7
commit 080afc99a8

@ -47,8 +47,7 @@ export default function visitIfBlock ( generator, block, state, node ) {
const params = block.params.join( ', ' ); const params = block.params.join( ', ' );
const name = generator.getUniqueName( `if_block` ); const name = generator.getUniqueName( `if_block` );
const getBlock = block.getUniqueName( `get_block` ); const getBlock = block.getUniqueName( `get_block` );
const currentBlock = block.getUniqueName( `current_block` ); const current_block = block.getUniqueName( `current_block` );
const _currentBlock = block.getUniqueName( `_current_block` );
const branches = getBranches( generator, block, state, node, generator.getUniqueName( `create_if_block` ) ); const branches = getBranches( generator, block, state, node, generator.getUniqueName( `create_if_block` ) );
const dynamic = branches.some( branch => branch.dynamic ); const dynamic = branches.some( branch => branch.dynamic );
@ -63,8 +62,8 @@ export default function visitIfBlock ( generator, block, state, node ) {
} ).join( '\n' )} } ).join( '\n' )}
} }
var ${currentBlock} = ${getBlock}( ${params} ); var ${current_block} = ${getBlock}( ${params} );
var ${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} ); var ${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
` ); ` );
const isToplevel = !state.parentNode; const isToplevel = !state.parentNode;
@ -75,26 +74,21 @@ export default function visitIfBlock ( generator, block, state, node ) {
block.builders.create.addLine( `if ( ${name} ) ${name}.mount( ${state.parentNode}, ${anchor} );` ); block.builders.create.addLine( `if ( ${name} ) ${name}.mount( ${state.parentNode}, ${anchor} );` );
} }
block.builders.update.addBlock( deindent`
var ${_currentBlock} = ${currentBlock};
${currentBlock} = ${getBlock}( ${params} );
` );
if ( dynamic ) { if ( dynamic ) {
block.builders.update.addBlock( deindent` block.builders.update.addBlock( deindent`
if ( ${_currentBlock} === ${currentBlock} && ${name} ) { if ( ${current_block} === ( ${current_block} = ${getBlock}( ${params} ) ) && ${name} ) {
${name}.update( changed, ${params} ); ${name}.update( changed, ${params} );
} else { } else {
if ( ${name} ) ${name}.destroy( true ); if ( ${name} ) ${name}.destroy( true );
${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} ); ${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} ); if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
} }
` ); ` );
} else { } else {
block.builders.update.addBlock( deindent` block.builders.update.addBlock( deindent`
if ( ${_currentBlock} !== ${currentBlock} ) { if ( ${current_block} !== ( ${current_block} = ${getBlock}( ${params} ) ) ) {
if ( ${name} ) ${name}.destroy( true ); if ( ${name} ) ${name}.destroy( true );
${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} ); ${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} ); if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
} }
` ); ` );

@ -18,10 +18,7 @@ function create_main_fragment ( root, component ) {
}, },
update: function ( changed, root ) { update: function ( changed, root ) {
var _current_block = current_block; if ( current_block !== ( current_block = get_block( root ) ) ) {
current_block = get_block( root );
if ( _current_block !== current_block ) {
if ( if_block ) if_block.destroy( true ); if ( if_block ) if_block.destroy( true );
if_block = current_block && current_block( root, component ); if_block = current_block && current_block( root, component );
if ( if_block ) if_block.mount( if_block_anchor.parentNode, if_block_anchor ); if ( if_block ) if_block.mount( if_block_anchor.parentNode, if_block_anchor );

Loading…
Cancel
Save