Skip to content

Commit

Permalink
simplify if-block switching code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 17, 2017
1 parent b7a4087 commit 080afc9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
20 changes: 7 additions & 13 deletions src/generators/dom/visitors/IfBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export default function visitIfBlock ( generator, block, state, node ) {
const params = block.params.join( ', ' );
const name = generator.getUniqueName( `if_block` );
const getBlock = block.getUniqueName( `get_block` );
const currentBlock = block.getUniqueName( `current_block` );
const _currentBlock = block.getUniqueName( `_current_block` );
const current_block = block.getUniqueName( `current_block` );

const branches = getBranches( generator, block, state, node, generator.getUniqueName( `create_if_block` ) );
const dynamic = branches.some( branch => branch.dynamic );
Expand All @@ -63,8 +62,8 @@ export default function visitIfBlock ( generator, block, state, node ) {
} ).join( '\n' )}
}
var ${currentBlock} = ${getBlock}( ${params} );
var ${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} );
var ${current_block} = ${getBlock}( ${params} );
var ${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
` );

const isToplevel = !state.parentNode;
Expand All @@ -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.update.addBlock( deindent`
var ${_currentBlock} = ${currentBlock};
${currentBlock} = ${getBlock}( ${params} );
` );

if ( dynamic ) {
block.builders.update.addBlock( deindent`
if ( ${_currentBlock} === ${currentBlock} && ${name} ) {
if ( ${current_block} === ( ${current_block} = ${getBlock}( ${params} ) ) && ${name} ) {
${name}.update( changed, ${params} );
} else {
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} );
}
` );
} else {
block.builders.update.addBlock( deindent`
if ( ${_currentBlock} !== ${currentBlock} ) {
if ( ${current_block} !== ( ${current_block} = ${getBlock}( ${params} ) ) ) {
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} );
}
` );
Expand Down
5 changes: 1 addition & 4 deletions test/js/samples/if-block-no-update/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ function create_main_fragment ( root, component ) {
},

update: function ( changed, root ) {
var _current_block = current_block;
current_block = get_block( root );

if ( _current_block !== current_block ) {
if ( current_block !== ( current_block = get_block( root ) ) ) {
if ( if_block ) if_block.destroy( true );
if_block = current_block && current_block( root, component );
if ( if_block ) if_block.mount( if_block_anchor.parentNode, if_block_anchor );
Expand Down

0 comments on commit 080afc9

Please sign in to comment.