Skip to content

Commit 080afc9

Browse files
committed
simplify if-block switching code
1 parent b7a4087 commit 080afc9

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

src/generators/dom/visitors/IfBlock.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export default function visitIfBlock ( generator, block, state, node ) {
4747
const params = block.params.join( ', ' );
4848
const name = generator.getUniqueName( `if_block` );
4949
const getBlock = block.getUniqueName( `get_block` );
50-
const currentBlock = block.getUniqueName( `current_block` );
51-
const _currentBlock = block.getUniqueName( `_current_block` );
50+
const current_block = block.getUniqueName( `current_block` );
5251

5352
const branches = getBranches( generator, block, state, node, generator.getUniqueName( `create_if_block` ) );
5453
const dynamic = branches.some( branch => branch.dynamic );
@@ -63,8 +62,8 @@ export default function visitIfBlock ( generator, block, state, node ) {
6362
} ).join( '\n' )}
6463
}
6564
66-
var ${currentBlock} = ${getBlock}( ${params} );
67-
var ${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} );
65+
var ${current_block} = ${getBlock}( ${params} );
66+
var ${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
6867
` );
6968

7069
const isToplevel = !state.parentNode;
@@ -75,26 +74,21 @@ export default function visitIfBlock ( generator, block, state, node ) {
7574
block.builders.create.addLine( `if ( ${name} ) ${name}.mount( ${state.parentNode}, ${anchor} );` );
7675
}
7776

78-
block.builders.update.addBlock( deindent`
79-
var ${_currentBlock} = ${currentBlock};
80-
${currentBlock} = ${getBlock}( ${params} );
81-
` );
82-
8377
if ( dynamic ) {
8478
block.builders.update.addBlock( deindent`
85-
if ( ${_currentBlock} === ${currentBlock} && ${name} ) {
79+
if ( ${current_block} === ( ${current_block} = ${getBlock}( ${params} ) ) && ${name} ) {
8680
${name}.update( changed, ${params} );
8781
} else {
8882
if ( ${name} ) ${name}.destroy( true );
89-
${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} );
83+
${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
9084
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
9185
}
9286
` );
9387
} else {
9488
block.builders.update.addBlock( deindent`
95-
if ( ${_currentBlock} !== ${currentBlock} ) {
89+
if ( ${current_block} !== ( ${current_block} = ${getBlock}( ${params} ) ) ) {
9690
if ( ${name} ) ${name}.destroy( true );
97-
${name} = ${currentBlock} && ${currentBlock}( ${params}, ${block.component} );
91+
${name} = ${current_block} && ${current_block}( ${params}, ${block.component} );
9892
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
9993
}
10094
` );

test/js/samples/if-block-no-update/expected.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ function create_main_fragment ( root, component ) {
1818
},
1919

2020
update: function ( changed, root ) {
21-
var _current_block = current_block;
22-
current_block = get_block( root );
23-
24-
if ( _current_block !== current_block ) {
21+
if ( current_block !== ( current_block = get_block( root ) ) ) {
2522
if ( if_block ) if_block.destroy( true );
2623
if_block = current_block && current_block( root, component );
2724
if ( if_block ) if_block.mount( if_block_anchor.parentNode, if_block_anchor );

0 commit comments

Comments
 (0)