DRY out block.tmp stuff

pull/453/head
Rich-Harris 8 years ago
parent 8148230e49
commit 67967b1a83

@ -104,17 +104,16 @@ export default class Block {
if ( this.builders.update.isEmpty() ) { if ( this.builders.update.isEmpty() ) {
properties.addBlock( `update: ${this.generator.helper( 'noop' )},` ); properties.addBlock( `update: ${this.generator.helper( 'noop' )},` );
} else { } else {
if ( this._tmp ) this.builders.update.addBlockAtStart( `var ${this._tmp};` );
properties.addBlock( deindent` properties.addBlock( deindent`
update: function ( changed, ${this.params.join( ', ' )} ) { update: function ( changed, ${this.params.join( ', ' )} ) {
${this.tmp ? `var ${this.tmp};` : ''}
${this.builders.update} ${this.builders.update}
}, },
` ); ` );
} }
if ( this.builders.destroy.isEmpty() ) { if ( this.builders.destroy.isEmpty() ) {
properties.addBlock( `destroy: ${this.generator.helper( 'noop' )},` ); properties.addBlock( `destroy: ${this.generator.helper( 'noop' )}` );
} else { } else {
properties.addBlock( deindent` properties.addBlock( deindent`
destroy: function ( detach ) { destroy: function ( detach ) {
@ -133,4 +132,9 @@ export default class Block {
} }
`; `;
} }
tmp () {
if ( !this._tmp ) this._tmp = this.getUniqueName( 'tmp' );
return this._tmp;
}
} }

@ -8,11 +8,9 @@ export default function visitMustacheTag ( generator, block, state, node ) {
block.builders.create.addLine( `var last_${name} = ${snippet};` ); block.builders.create.addLine( `var last_${name} = ${snippet};` );
block.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, state.parentNode, true ); block.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, state.parentNode, true );
if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' );
block.builders.update.addBlock( deindent` block.builders.update.addBlock( deindent`
if ( ( ${block.tmp} = ${snippet} ) !== last_${name} ) { if ( ( ${block.tmp()} = ${snippet} ) !== last_${name} ) {
${name}.data = last_${name} = ${block.tmp}; ${name}.data = last_${name} = ${block.tmp()};
} }
` ); ` );
} }

@ -25,11 +25,9 @@ export default function visitRawMustacheTag ( generator, block, state, node ) {
block.builders.create.addLine( mountStatement ); block.builders.create.addLine( mountStatement );
} }
if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' );
block.builders.update.addBlock( deindent` block.builders.update.addBlock( deindent`
if ( ( ${block.tmp} = ${snippet} ) !== last_${name} ) { if ( ( ${block.tmp()} = ${snippet} ) !== last_${name} ) {
last_${name} = ${block.tmp}; last_${name} = ${block.tmp()};
${detachStatement} ${detachStatement}
${mountStatement} ${mountStatement}
} }

@ -106,11 +106,9 @@ export default function addElementAttributes ( generator, block, node, local ) {
local.create.addLine( updater ); local.create.addLine( updater );
if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' );
local.update.addBlock( deindent` local.update.addBlock( deindent`
if ( ( ${block.tmp} = ${snippet} ) !== ${last} ) { if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) {
${last} = ${block.tmp}; ${last} = ${block.tmp()};
${updater} ${updater}
} }
` ); ` );

Loading…
Cancel
Save