0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

DRY out block.tmp stuff

This commit is contained in:
Rich-Harris 2017-04-08 17:22:52 -04:00
parent 8148230e49
commit 67967b1a83
4 changed files with 13 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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