more consistent naming

pull/453/head
Rich Harris 8 years ago
parent 11033c2f20
commit 3006a245af

@ -20,13 +20,13 @@ class DomGenerator extends Generator {
addElement ( name, renderStatement, needsIdentifier = false ) { addElement ( name, renderStatement, needsIdentifier = false ) {
const isToplevel = this.current.localElementDepth === 0; const isToplevel = this.current.localElementDepth === 0;
if ( needsIdentifier || isToplevel ) { if ( needsIdentifier || isToplevel ) {
this.current.builders.init.addLine( this.current.builders.create.addLine(
`var ${name} = ${renderStatement};` `var ${name} = ${renderStatement};`
); );
this.createMountStatement( name ); this.createMountStatement( name );
} else { } else {
this.current.builders.init.addLine( `${this.helper( 'appendNode' )}( ${renderStatement}, ${this.current.target} );` ); this.current.builders.create.addLine( `${this.helper( 'appendNode' )}( ${renderStatement}, ${this.current.target} );` );
} }
if ( isToplevel ) { if ( isToplevel ) {
@ -36,7 +36,7 @@ class DomGenerator extends Generator {
addRenderer ( fragment ) { addRenderer ( fragment ) {
if ( fragment.autofocus ) { if ( fragment.autofocus ) {
fragment.builders.init.addLine( `${fragment.autofocus}.focus();` ); fragment.builders.create.addLine( `${fragment.autofocus}.focus();` );
} }
// minor hack we need to ensure that any {{{triples}}} are detached // minor hack we need to ensure that any {{{triples}}} are detached
@ -44,7 +44,7 @@ class DomGenerator extends Generator {
fragment.builders.detachRaw.addBlock( fragment.builders.detach ); fragment.builders.detachRaw.addBlock( fragment.builders.detach );
if ( !fragment.builders.detachRaw.isEmpty() ) { if ( !fragment.builders.detachRaw.isEmpty() ) {
fragment.builders.teardown.addBlock( deindent` fragment.builders.destroy.addBlock( deindent`
if ( detach ) { if ( detach ) {
${fragment.builders.detachRaw} ${fragment.builders.detachRaw}
} }
@ -81,19 +81,19 @@ class DomGenerator extends Generator {
` ); ` );
} }
if ( fragment.builders.teardown.isEmpty() ) { if ( fragment.builders.destroy.isEmpty() ) {
properties.addBlock( `teardown: ${this.helper( 'noop' )},` ); properties.addBlock( `destroy: ${this.helper( 'noop' )},` );
} else { } else {
properties.addBlock( deindent` properties.addBlock( deindent`
teardown: function ( detach ) { destroy: function ( detach ) {
${fragment.builders.teardown} ${fragment.builders.destroy}
} }
` ); ` );
} }
this.renderers.push( deindent` this.renderers.push( deindent`
function ${fragment.name} ( ${fragment.params.join( ', ' )}, ${fragment.component}${fragment.key ? `, ${localKey}` : ''} ) { function ${fragment.name} ( ${fragment.params.join( ', ' )}, ${fragment.component}${fragment.key ? `, ${localKey}` : ''} ) {
${fragment.builders.init} ${fragment.builders.create}
return { return {
${properties} ${properties}
@ -111,7 +111,7 @@ class DomGenerator extends Generator {
if ( this.current.target === 'target' ) { if ( this.current.target === 'target' ) {
this.current.builders.mount.addLine( `${this.helper( 'insertNode' )}( ${name}, target, anchor );` ); this.current.builders.mount.addLine( `${this.helper( 'insertNode' )}( ${name}, target, anchor );` );
} else { } else {
this.current.builders.init.addLine( `${this.helper( 'appendNode' )}( ${name}, ${this.current.target} );` ); this.current.builders.create.addLine( `${this.helper( 'appendNode' )}( ${name}, ${this.current.target} );` );
} }
} }
@ -158,7 +158,7 @@ export default function dom ( parsed, source, options ) {
generator.push({ generator.push({
type: 'block', type: 'block',
name: generator.alias( 'render_main_fragment' ), name: generator.alias( 'create_main_fragment' ),
namespace, namespace,
target: 'target', target: 'target',
localElementDepth: 0, localElementDepth: 0,
@ -261,7 +261,7 @@ export default function dom ( parsed, source, options ) {
if ( generator.hasComplexBindings ) { if ( generator.hasComplexBindings ) {
builders.init.addBlock( deindent` builders.init.addBlock( deindent`
this._bindings = []; this._bindings = [];
this._fragment = ${generator.alias( 'render_main_fragment' )}( this._state, this ); this._fragment = ${generator.alias( 'create_main_fragment' )}( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null ); if ( options.target ) this._fragment.mount( options.target, null );
while ( this._bindings.length ) this._bindings.pop()(); while ( this._bindings.length ) this._bindings.pop()();
` ); ` );
@ -269,7 +269,7 @@ export default function dom ( parsed, source, options ) {
builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` ); builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` );
} else { } else {
builders.init.addBlock( deindent` builders.init.addBlock( deindent`
this._fragment = ${generator.alias( 'render_main_fragment' )}( this._state, this ); this._fragment = ${generator.alias( 'create_main_fragment' )}( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null ); if ( options.target ) this._fragment.mount( options.target, null );
` ); ` );
} }
@ -369,7 +369,7 @@ export default function dom ( parsed, source, options ) {
${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) { ${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) {
this.fire( 'destroy' );${templateProperties.ondestroy ? `\n${generator.alias( 'template' )}.ondestroy.call( this );` : ``} this.fire( 'destroy' );${templateProperties.ondestroy ? `\n${generator.alias( 'template' )}.ondestroy.call( this );` : ``}
this._fragment.teardown( detach !== false ); this._fragment.destroy( detach !== false );
this._fragment = null; this._fragment = null;
this._state = {}; this._state = {};

@ -2,11 +2,11 @@ import CodeBuilder from '../../../utils/CodeBuilder.js';
export default function getBuilders () { export default function getBuilders () {
return { return {
init: new CodeBuilder(), create: new CodeBuilder(),
mount: new CodeBuilder(), mount: new CodeBuilder(),
update: new CodeBuilder(), update: new CodeBuilder(),
detach: new CodeBuilder(), detach: new CodeBuilder(),
detachRaw: new CodeBuilder(), detachRaw: new CodeBuilder(),
teardown: new CodeBuilder() destroy: new CodeBuilder()
}; };
} }

@ -31,7 +31,7 @@ export default function visitComponent ( generator, node ) {
allUsedContexts: [], allUsedContexts: [],
init: new CodeBuilder(), create: new CodeBuilder(),
update: new CodeBuilder() update: new CodeBuilder()
}; };
@ -60,7 +60,7 @@ export default function visitComponent ( generator, node ) {
return `${name}._context.${listName} = ${listName};\n${name}._context.${indexName} = ${indexName};`; return `${name}._context.${listName} = ${listName};\n${name}._context.${indexName} = ${indexName};`;
}).join( '\n' ); }).join( '\n' );
local.init.addBlock( deindent` local.create.addBlock( deindent`
${name}._context = { ${name}._context = {
${initialProps} ${initialProps}
}; };
@ -83,7 +83,7 @@ export default function visitComponent ( generator, node ) {
const yieldFragment = current.getUniqueName( `${name}_yield_fragment` ); const yieldFragment = current.getUniqueName( `${name}_yield_fragment` );
current.builders.init.addLine( current.builders.create.addLine(
`var ${yieldFragment} = ${yieldName}( ${params}, ${current.component} );` `var ${yieldFragment} = ${yieldName}( ${params}, ${current.component} );`
); );
@ -120,7 +120,7 @@ export default function visitComponent ( generator, node ) {
const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`; const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`;
local.init.addBlockAtStart( deindent` local.create.addBlockAtStart( deindent`
${statements.join( '\n' )} ${statements.join( '\n' )}
var ${name} = new ${expression}({ var ${name} = new ${expression}({
${componentInitProperties.join(',\n')} ${componentInitProperties.join(',\n')}
@ -153,9 +153,9 @@ export default function visitComponent ( generator, node ) {
` ); ` );
} }
current.builders.teardown.addLine( `${name}.destroy( ${isToplevel ? 'detach' : 'false'} );` ); current.builders.destroy.addLine( `${name}.destroy( ${isToplevel ? 'detach' : 'false'} );` );
current.builders.init.addBlock( local.init ); current.builders.create.addBlock( local.create );
if ( !local.update.isEmpty() ) current.builders.update.addBlock( local.update ); if ( !local.update.isEmpty() ) current.builders.update.addBlock( local.update );
generator.push({ generator.push({

@ -30,10 +30,10 @@ export default function visitEachBlock ( generator, node ) {
localVars.lookup = generator.current.getUniqueName( `${name}_lookup` ); localVars.lookup = generator.current.getUniqueName( `${name}_lookup` );
localVars._lookup = generator.current.getUniqueName( `_${name}_lookup` ); localVars._lookup = generator.current.getUniqueName( `_${name}_lookup` );
generator.current.builders.init.addLine( `var ${listName} = ${snippet};` ); generator.current.builders.create.addLine( `var ${listName} = ${snippet};` );
generator.current.builders.init.addLine( `var ${localVars.iterations} = [];` ); generator.current.builders.create.addLine( `var ${localVars.iterations} = [];` );
if ( node.key ) generator.current.builders.init.addLine( `var ${localVars.lookup} = Object.create( null );` ); if ( node.key ) generator.current.builders.create.addLine( `var ${localVars.lookup} = Object.create( null );` );
if ( node.else ) generator.current.builders.init.addLine( `var ${elseName} = null;` ); if ( node.else ) generator.current.builders.create.addLine( `var ${elseName} = null;` );
const initialRender = new CodeBuilder(); const initialRender = new CodeBuilder();
@ -58,14 +58,14 @@ export default function visitEachBlock ( generator, node ) {
); );
} }
generator.current.builders.init.addBlock( deindent` generator.current.builders.create.addBlock( deindent`
for ( var ${i} = 0; ${i} < ${listName}.length; ${i} += 1 ) { for ( var ${i} = 0; ${i} < ${listName}.length; ${i} += 1 ) {
${initialRender} ${initialRender}
} }
` ); ` );
if ( node.else ) { if ( node.else ) {
generator.current.builders.init.addBlock( deindent` generator.current.builders.create.addBlock( deindent`
if ( !${listName}.length ) { if ( !${listName}.length ) {
${elseName} = ${renderElse}( ${params}, ${generator.current.component} ); ${elseName} = ${renderElse}( ${params}, ${generator.current.component} );
${!isToplevel ? `${elseName}.mount( ${anchor}.parentNode, ${anchor} );` : ''} ${!isToplevel ? `${elseName}.mount( ${anchor}.parentNode, ${anchor} );` : ''}
@ -115,7 +115,7 @@ export default function visitEachBlock ( generator, node ) {
for ( var ${i} = 0; ${i} < ${localVars.iterations}.length; ${i} += 1 ) { for ( var ${i} = 0; ${i} < ${localVars.iterations}.length; ${i} += 1 ) {
var ${localVars.iteration} = ${localVars.iterations}[${i}]; var ${localVars.iteration} = ${localVars.iterations}[${i}];
if ( !${localVars._lookup}[ ${localVars.iteration}.key ] ) { if ( !${localVars._lookup}[ ${localVars.iteration}.key ] ) {
${localVars.iteration}.teardown( true ); ${localVars.iteration}.destroy( true );
} }
} }
@ -137,7 +137,7 @@ export default function visitEachBlock ( generator, node ) {
} }
} }
teardownEach( ${localVars.iterations}, true, ${listName}.length ); destroyEach( ${localVars.iterations}, true, ${listName}.length );
${localVars.iterations}.length = ${listName}.length; ${localVars.iterations}.length = ${listName}.length;
` ); ` );
@ -151,18 +151,18 @@ export default function visitEachBlock ( generator, node ) {
${elseName} = ${renderElse}( ${params}, ${generator.current.component} ); ${elseName} = ${renderElse}( ${params}, ${generator.current.component} );
${elseName}.mount( ${anchor}.parentNode, ${anchor} ); ${elseName}.mount( ${anchor}.parentNode, ${anchor} );
} else if ( ${elseName} ) { } else if ( ${elseName} ) {
${elseName}.teardown( true ); ${elseName}.destroy( true );
} }
` ); ` );
} }
generator.current.builders.teardown.addBlock( generator.current.builders.destroy.addBlock(
`${generator.helper( 'teardownEach' )}( ${localVars.iterations}, ${isToplevel ? 'detach' : 'false'} );` ); `${generator.helper( 'destroyEach' )}( ${localVars.iterations}, ${isToplevel ? 'detach' : 'false'} );` );
if ( node.else ) { if ( node.else ) {
generator.current.builders.teardown.addBlock( deindent` generator.current.builders.destroy.addBlock( deindent`
if ( ${elseName} ) { if ( ${elseName} ) {
${elseName}.teardown( ${isToplevel ? 'detach' : 'false'} ); ${elseName}.destroy( ${isToplevel ? 'detach' : 'false'} );
} }
` ); ` );
} }

@ -27,9 +27,9 @@ export default function visitElement ( generator, node ) {
allUsedContexts: [], allUsedContexts: [],
init: new CodeBuilder(), create: new CodeBuilder(),
update: new CodeBuilder(), update: new CodeBuilder(),
teardown: new CodeBuilder() destroy: new CodeBuilder()
}; };
const isToplevel = generator.current.localElementDepth === 0; const isToplevel = generator.current.localElementDepth === 0;
@ -55,7 +55,7 @@ export default function visitElement ( generator, node ) {
return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`; return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`;
}).join( '\n' ); }).join( '\n' );
local.init.addBlock( deindent` local.create.addBlock( deindent`
${name}.__svelte = { ${name}.__svelte = {
${initialProps} ${initialProps}
}; };
@ -80,7 +80,7 @@ export default function visitElement ( generator, node ) {
render += `\n${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );`; render += `\n${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );`;
} }
local.init.addLineAtStart( render ); local.create.addLineAtStart( render );
if ( isToplevel ) { if ( isToplevel ) {
generator.current.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` ); generator.current.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` );
} }
@ -92,9 +92,9 @@ export default function visitElement ( generator, node ) {
node.initialUpdate = statement; node.initialUpdate = statement;
} }
generator.current.builders.init.addBlock( local.init ); generator.current.builders.create.addBlock( local.create );
if ( !local.update.isEmpty() ) generator.current.builders.update.addBlock( local.update ); if ( !local.update.isEmpty() ) generator.current.builders.update.addBlock( local.update );
if ( !local.teardown.isEmpty() ) generator.current.builders.teardown.addBlock( local.teardown ); if ( !local.destroy.isEmpty() ) generator.current.builders.destroy.addBlock( local.destroy );
generator.createMountStatement( name ); generator.createMountStatement( name );
@ -116,7 +116,7 @@ export default function visitElement ( generator, node ) {
generator.elementDepth -= 1; generator.elementDepth -= 1;
if ( node.initialUpdate ) { if ( node.initialUpdate ) {
generator.current.builders.init.addBlock( node.initialUpdate ); generator.current.builders.create.addBlock( node.initialUpdate );
} }
generator.pop(); generator.pop();

@ -43,7 +43,7 @@ export default function visitIfBlock ( generator, node ) {
const anchor = `${name}_anchor`; const anchor = `${name}_anchor`;
generator.createAnchor( anchor ); generator.createAnchor( anchor );
generator.current.builders.init.addBlock( deindent` generator.current.builders.create.addBlock( deindent`
function ${getBlock} ( ${params} ) { function ${getBlock} ( ${params} ) {
${conditionsAndBlocks.map( ({ condition, block }) => { ${conditionsAndBlocks.map( ({ condition, block }) => {
return `${condition ? `if ( ${condition} ) ` : ''}return ${block};`; return `${condition ? `if ( ${condition} ) ` : ''}return ${block};`;
@ -58,7 +58,7 @@ export default function visitIfBlock ( generator, node ) {
if ( isToplevel ) { if ( isToplevel ) {
generator.current.builders.mount.addLine( mountStatement ); generator.current.builders.mount.addLine( mountStatement );
} else { } else {
generator.current.builders.init.addLine( mountStatement ); generator.current.builders.create.addLine( mountStatement );
} }
generator.current.builders.update.addBlock( deindent` generator.current.builders.update.addBlock( deindent`
@ -67,13 +67,13 @@ export default function visitIfBlock ( generator, node ) {
if ( ${_currentBlock} === ${currentBlock} && ${name}) { if ( ${_currentBlock} === ${currentBlock} && ${name}) {
${name}.update( changed, ${params} ); ${name}.update( changed, ${params} );
} else { } else {
if ( ${name} ) ${name}.teardown( true ); if ( ${name} ) ${name}.destroy( true );
${name} = ${currentBlock} && ${currentBlock}( ${params}, ${generator.current.component} ); ${name} = ${currentBlock} && ${currentBlock}( ${params}, ${generator.current.component} );
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} ); if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
} }
` ); ` );
generator.current.builders.teardown.addLine( generator.current.builders.destroy.addLine(
`if ( ${name} ) ${name}.teardown( ${isToplevel ? 'detach' : 'false'} );` `if ( ${name} ) ${name}.destroy( ${isToplevel ? 'detach' : 'false'} );`
); );
} }

@ -6,7 +6,7 @@ export default function visitMustacheTag ( generator, node ) {
const { snippet } = generator.contextualise( node.expression ); const { snippet } = generator.contextualise( node.expression );
generator.current.builders.init.addLine( `var last_${name} = ${snippet};` ); generator.current.builders.create.addLine( `var last_${name} = ${snippet};` );
generator.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, true ); generator.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, true );
const fragment = findBlock( generator.current ); const fragment = findBlock( generator.current );

@ -16,14 +16,14 @@ export default function visitRawMustacheTag ( generator, node ) {
const isToplevel = generator.current.localElementDepth === 0; const isToplevel = generator.current.localElementDepth === 0;
generator.current.builders.init.addLine( `var last_${name} = ${snippet};` ); generator.current.builders.create.addLine( `var last_${name} = ${snippet};` );
const mountStatement = `${before}.insertAdjacentHTML( 'afterend', last_${name} );`; const mountStatement = `${before}.insertAdjacentHTML( 'afterend', last_${name} );`;
const detachStatement = `${generator.helper( 'detachBetween' )}( ${before}, ${after} );`; const detachStatement = `${generator.helper( 'detachBetween' )}( ${before}, ${after} );`;
if ( isToplevel ) { if ( isToplevel ) {
generator.current.builders.mount.addLine( mountStatement ); generator.current.builders.mount.addLine( mountStatement );
} else { } else {
generator.current.builders.init.addLine( mountStatement ); generator.current.builders.create.addLine( mountStatement );
} }
const fragment = findBlock( generator.current ); const fragment = findBlock( generator.current );

@ -6,7 +6,7 @@ export default function visitYieldTag ( generator ) {
`${generator.current.component}._yield && ${generator.current.component}._yield.mount( ${generator.current.target}, ${anchor} );` `${generator.current.component}._yield && ${generator.current.component}._yield.mount( ${generator.current.target}, ${anchor} );`
); );
generator.current.builders.teardown.addLine( generator.current.builders.destroy.addLine(
`${generator.current.component}._yield && ${generator.current.component}._yield.teardown( detach );` `${generator.current.component}._yield && ${generator.current.component}._yield.destroy( detach );`
); );
} }

@ -102,7 +102,7 @@ export default function addComponentAttributes ( generator, node, local ) {
const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`; const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`;
local.init.addBlock( deindent` local.create.addBlock( deindent`
${local.name}.on( '${attribute.name}', function ( event ) { ${local.name}.on( '${attribute.name}', function ( event ) {
${handlerBody} ${handlerBody}
}); });
@ -116,11 +116,11 @@ export default function addComponentAttributes ( generator, node, local ) {
else if ( attribute.type === 'Ref' ) { else if ( attribute.type === 'Ref' ) {
generator.usesRefs = true; generator.usesRefs = true;
local.init.addLine( local.create.addLine(
`${generator.current.component}.refs.${attribute.name} = ${local.name};` `${generator.current.component}.refs.${attribute.name} = ${local.name};`
); );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.destroy.addLine( deindent`
if ( ${generator.current.component}.refs.${attribute.name} === ${local.name} ) ${generator.current.component}.refs.${attribute.name} = null; if ( ${generator.current.component}.refs.${attribute.name} === ${local.name} ) ${generator.current.component}.refs.${attribute.name} = null;
` ); ` );
} }

@ -41,7 +41,7 @@ export default function createBinding ( generator, node, attribute, current, loc
const updating = generator.current.getUniqueName( `${local.name}_updating` ); const updating = generator.current.getUniqueName( `${local.name}_updating` );
local.init.addBlock( deindent` local.create.addBlock( deindent`
var ${updating} = false; var ${updating} = false;
${generator.current.component}._bindings.push( function () { ${generator.current.component}._bindings.push( function () {

@ -32,11 +32,11 @@ export default function addElementAttributes ( generator, node, local ) {
if ( attribute.value === true ) { if ( attribute.value === true ) {
// attributes without values, e.g. <textarea readonly> // attributes without values, e.g. <textarea readonly>
if ( propertyName ) { if ( propertyName ) {
local.init.addLine( local.create.addLine(
`${local.name}.${propertyName} = true;` `${local.name}.${propertyName} = true;`
); );
} else { } else {
local.init.addLine( local.create.addLine(
`${generator.helper( method )}( ${local.name}, '${name}', true );` `${generator.helper( method )}( ${local.name}, '${name}', true );`
); );
} }
@ -49,11 +49,11 @@ export default function addElementAttributes ( generator, node, local ) {
else if ( attribute.value.length === 0 ) { else if ( attribute.value.length === 0 ) {
if ( propertyName ) { if ( propertyName ) {
local.init.addLine( local.create.addLine(
`${local.name}.${propertyName} = '';` `${local.name}.${propertyName} = '';`
); );
} else { } else {
local.init.addLine( local.create.addLine(
`${generator.helper( method )}( ${local.name}, '${name}', '' );` `${generator.helper( method )}( ${local.name}, '${name}', '' );`
); );
} }
@ -75,7 +75,7 @@ export default function addElementAttributes ( generator, node, local ) {
local.namespace = value.data; local.namespace = value.data;
addAttribute = true; addAttribute = true;
} else if ( propertyName ) { } else if ( propertyName ) {
local.init.addLine( local.create.addLine(
`${local.name}.${propertyName} = ${result};` `${local.name}.${propertyName} = ${result};`
); );
} else { } else {
@ -83,7 +83,7 @@ export default function addElementAttributes ( generator, node, local ) {
} }
if ( addAttribute ) { if ( addAttribute ) {
local.init.addLine( local.create.addLine(
`${generator.helper( method )}( ${local.name}, '${name}', ${result} );` `${generator.helper( method )}( ${local.name}, '${name}', ${result} );`
); );
} }
@ -96,7 +96,7 @@ export default function addElementAttributes ( generator, node, local ) {
const { snippet } = generator.contextualise( value.expression ); const { snippet } = generator.contextualise( value.expression );
const last = `last_${local.name}_${name.replace( /-/g, '_')}`; const last = `last_${local.name}_${name.replace( /-/g, '_')}`;
local.init.addLine( `var ${last} = ${snippet};` ); local.create.addLine( `var ${last} = ${snippet};` );
let updater; let updater;
if ( propertyName ) { if ( propertyName ) {
@ -105,7 +105,7 @@ export default function addElementAttributes ( generator, node, local ) {
updater = `${generator.helper( method )}( ${local.name}, '${name}', ${last} );`; updater = `${generator.helper( method )}( ${local.name}, '${name}', ${last} );`;
} }
local.init.addLine( updater ); local.create.addLine( updater );
const fragment = findBlock( generator.current ); const fragment = findBlock( generator.current );
if ( !fragment.tmp ) fragment.tmp = fragment.getUniqueName( 'tmp' ); if ( !fragment.tmp ) fragment.tmp = fragment.getUniqueName( 'tmp' );
@ -139,14 +139,14 @@ export default function addElementAttributes ( generator, node, local ) {
updater = `${generator.helper( method )}( ${local.name}, '${name}', ${value} );`; updater = `${generator.helper( method )}( ${local.name}, '${name}', ${value} );`;
} }
local.init.addLine( updater ); local.create.addLine( updater );
local.update.addLine( updater ); local.update.addLine( updater );
} }
if ( isIndirectlyBoundValue ) { if ( isIndirectlyBoundValue ) {
const updateValue = `${local.name}.value = ${local.name}.__value;`; const updateValue = `${local.name}.value = ${local.name}.__value;`;
local.init.addLine( updateValue ); local.create.addLine( updateValue );
if ( dynamic ) local.update.addLine( updateValue ); if ( dynamic ) local.update.addLine( updateValue );
} }
} }
@ -185,17 +185,17 @@ export default function addElementAttributes ( generator, node, local ) {
const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`; const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`;
if ( generator.events.has( name ) ) { if ( generator.events.has( name ) ) {
local.init.addBlock( deindent` local.create.addBlock( deindent`
var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${generator.current.component}, ${local.name}, function ( event ) { var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${generator.current.component}, ${local.name}, function ( event ) {
${handlerBody} ${handlerBody}
}.bind( ${local.name} ) ); }.bind( ${local.name} ) );
` ); ` );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.destroy.addLine( deindent`
${handlerName}.teardown(); ${handlerName}.teardown();
` ); ` );
} else { } else {
local.init.addBlock( deindent` local.create.addBlock( deindent`
function ${handlerName} ( event ) { function ${handlerName} ( event ) {
${handlerBody} ${handlerBody}
} }
@ -203,7 +203,7 @@ export default function addElementAttributes ( generator, node, local ) {
${generator.helper( 'addEventListener' )}( ${local.name}, '${name}', ${handlerName} ); ${generator.helper( 'addEventListener' )}( ${local.name}, '${name}', ${handlerName} );
` ); ` );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.destroy.addLine( deindent`
${generator.helper( 'removeEventListener' )}( ${local.name}, '${name}', ${handlerName} ); ${generator.helper( 'removeEventListener' )}( ${local.name}, '${name}', ${handlerName} );
` ); ` );
} }
@ -216,11 +216,11 @@ export default function addElementAttributes ( generator, node, local ) {
else if ( attribute.type === 'Ref' ) { else if ( attribute.type === 'Ref' ) {
generator.usesRefs = true; generator.usesRefs = true;
local.init.addLine( local.create.addLine(
`${generator.current.component}.refs.${name} = ${local.name};` `${generator.current.component}.refs.${name} = ${local.name};`
); );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.destroy.addLine( deindent`
if ( ${generator.current.component}.refs.${name} === ${local.name} ) ${generator.current.component}.refs.${name} = null; if ( ${generator.current.component}.refs.${name} === ${local.name} ) ${generator.current.component}.refs.${name} = null;
` ); ` );
} }

@ -66,11 +66,11 @@ export default function createBinding ( generator, node, attribute, current, loc
`~${snippet}.indexOf( ${local.name}.__value )` : `~${snippet}.indexOf( ${local.name}.__value )` :
`${local.name}.__value === ${snippet}`; `${local.name}.__value === ${snippet}`;
local.init.addLine( local.create.addLine(
`${current.component}._bindingGroups[${bindingGroup}].push( ${local.name} );` `${current.component}._bindingGroups[${bindingGroup}].push( ${local.name} );`
); );
local.teardown.addBlock( local.destroy.addBlock(
`${current.component}._bindingGroups[${bindingGroup}].splice( ${current.component}._bindingGroups[${bindingGroup}].indexOf( ${local.name} ), 1 );` `${current.component}._bindingGroups[${bindingGroup}].splice( ${current.component}._bindingGroups[${bindingGroup}].indexOf( ${local.name} ), 1 );`
); );
@ -84,7 +84,7 @@ export default function createBinding ( generator, node, attribute, current, loc
const updating = generator.current.getUniqueName( `${local.name}_updating` ); const updating = generator.current.getUniqueName( `${local.name}_updating` );
local.init.addBlock( deindent` local.create.addBlock( deindent`
var ${updating} = false; var ${updating} = false;
function ${handler} () { function ${handler} () {
@ -104,7 +104,7 @@ export default function createBinding ( generator, node, attribute, current, loc
} }
` ); ` );
current.builders.teardown.addLine( deindent` current.builders.destroy.addLine( deindent`
${generator.helper( 'removeEventListener' )}( ${local.name}, '${eventName}', ${handler} ); ${generator.helper( 'removeEventListener' )}( ${local.name}, '${eventName}', ${handler} );
` ); ` );
} }

@ -27,14 +27,14 @@ export default function visitWindow ( generator, node ) {
const handlerName = generator.current.getUniqueName( `onwindow${attribute.name}` ); const handlerName = generator.current.getUniqueName( `onwindow${attribute.name}` );
generator.current.builders.init.addBlock( deindent` generator.current.builders.create.addBlock( deindent`
var ${handlerName} = function ( event ) { var ${handlerName} = function ( event ) {
[${attribute.expression.start}-${attribute.expression.end}]; [${attribute.expression.start}-${attribute.expression.end}];
}; };
window.addEventListener( '${attribute.name}', ${handlerName} ); window.addEventListener( '${attribute.name}', ${handlerName} );
` ); ` );
generator.current.builders.teardown.addBlock( deindent` generator.current.builders.destroy.addBlock( deindent`
window.removeEventListener( '${attribute.name}', ${handlerName} ); window.removeEventListener( '${attribute.name}', ${handlerName} );
` ); ` );
} }
@ -66,7 +66,7 @@ export default function visitWindow ( generator, node ) {
const props = events[ event ].join( ',\n' ); const props = events[ event ].join( ',\n' );
generator.current.builders.init.addBlock( deindent` generator.current.builders.create.addBlock( deindent`
var ${handlerName} = function ( event ) { var ${handlerName} = function ( event ) {
component.set({ component.set({
${props} ${props}
@ -75,7 +75,7 @@ export default function visitWindow ( generator, node ) {
window.addEventListener( '${event}', ${handlerName} ); window.addEventListener( '${event}', ${handlerName} );
` ); ` );
generator.current.builders.teardown.addBlock( deindent` generator.current.builders.destroy.addBlock( deindent`
window.removeEventListener( '${event}', ${handlerName} ); window.removeEventListener( '${event}', ${handlerName} );
` ); ` );
}); });

@ -16,9 +16,9 @@ export function detachBetween ( before, after ) {
} }
} }
export function teardownEach ( iterations, detach, start ) { export function destroyEach ( iterations, detach, start ) {
for ( var i = ( start || 0 ); i < iterations.length; i += 1 ) { for ( var i = ( start || 0 ); i < iterations.length; i += 1 ) {
iterations[i].teardown( detach ); iterations[i].destroy( detach );
} }
} }

@ -2,7 +2,7 @@ import checkForDupes from '../utils/checkForDupes.js';
import checkForComputedKeys from '../utils/checkForComputedKeys.js'; import checkForComputedKeys from '../utils/checkForComputedKeys.js';
import usesThisOrArguments from '../utils/usesThisOrArguments.js'; import usesThisOrArguments from '../utils/usesThisOrArguments.js';
const builtin = new Set( [ 'set', 'get', 'on', 'fire', 'observe', 'teardown' ] ); const builtin = new Set( [ 'set', 'get', 'on', 'fire', 'observe', 'destroy' ] );
export default function methods ( validator, prop ) { export default function methods ( validator, prop ) {
if ( prop.value.type !== 'ObjectExpression' ) { if ( prop.value.type !== 'ObjectExpression' ) {

Loading…
Cancel
Save