fix/simplify bindings

pull/1367/head
Rich Harris 7 years ago
parent c62178db3e
commit b35a9e674f

@ -182,22 +182,10 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`); this.builders.mount.addLine(`${this.autofocus}.focus();`);
} }
// TODO `this.contexts` is possibly redundant post-#1122 if (this.maintainContext) {
const initializers = []; this.builders.update.addLineAtStart(`ctx = _ctx;`);
this.contexts.forEach((name, context) => {
// TODO only the ones that are actually used in this block...
const listName = this.listNames.get(context);
const indexName = this.indexNames.get(context);
initializers.push(
`${name} = ctx.${context}`,
`${listName} = ctx.${listName}`,
`${indexName} = ctx.${indexName}`
);
this.hasUpdateMethod = true; this.hasUpdateMethod = true;
}); }
// minor hack we need to ensure that any {{{triples}}} are detached first // minor hack we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString()); this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());
@ -258,11 +246,11 @@ export default class Block {
} }
if (this.hasUpdateMethod) { if (this.hasUpdateMethod) {
if (this.builders.update.isEmpty() && initializers.length === 0) { if (this.builders.update.isEmpty()) {
properties.addBlock(`p: @noop,`); properties.addBlock(`p: @noop,`);
} else { } else {
properties.addBlock(deindent` properties.addBlock(deindent`
p: function update(changed, ctx) { p: function update(changed, ${this.maintainContext ? '_ctx' : 'ctx'}) {
${this.builders.update} ${this.builders.update}
}, },
`); `);
@ -334,8 +322,6 @@ export default class Block {
return deindent` return deindent`
${this.comment && `// ${escape(this.comment)}`} ${this.comment && `// ${escape(this.comment)}`}
function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, ctx) { function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, ctx) {
${initializers.length > 0 &&
`var ${initializers.join(', ')};`}
${this.variables.size > 0 && ${this.variables.size > 0 &&
`var ${Array.from(this.variables.keys()) `var ${Array.from(this.variables.keys())
.map(key => { .map(key => {

@ -198,8 +198,8 @@ function getEventHandler(
? getTailSnippet(binding.value.node) ? getTailSnippet(binding.value.node)
: ''; : '';
const list = `context.${block.listNames.get(name)}`; const list = `ctx.${block.listNames.get(name)}`;
const index = `context.${block.indexNames.get(name)}`; const index = `ctx.${block.indexNames.get(name)}`;
return { return {
usesContext: true, usesContext: true,

@ -301,7 +301,7 @@ export default class Element extends Node {
} }
let hasHoistedEventHandlerOrBinding = ( let hasHoistedEventHandlerOrBinding = (
(this.hasAncestor('EachBlock') && this.bindings.length > 0) || //(this.hasAncestor('EachBlock') && this.bindings.length > 0) ||
this.handlers.some(handler => handler.shouldHoist) this.handlers.some(handler => handler.shouldHoist)
); );
let eventHandlerOrBindingUsesComponent; let eventHandlerOrBindingUsesComponent;
@ -334,6 +334,10 @@ export default class Element extends Node {
${name}._svelte = { ${initialProps.join(', ')} }; ${name}._svelte = { ${initialProps.join(', ')} };
`); `);
} }
} else {
if (eventHandlerOrBindingUsesContext) {
block.maintainContext = true;
}
} }
this.addBindings(block, allUsedContexts); this.addBindings(block, allUsedContexts);
@ -457,8 +461,6 @@ export default class Element extends Node {
cancelAnimationFrame(${animation_frame}); cancelAnimationFrame(${animation_frame});
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});` if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`
} }
${usesContext && `var context = ${this.var}._svelte;`}
${usesState && `var ctx = #component.get();`}
${usesStore && `var $ = #component.store.get();`} ${usesStore && `var $ = #component.store.get();`}
${needsLock && `${lock} = true;`} ${needsLock && `${lock} = true;`}
${mutations.length > 0 && mutations} ${mutations.length > 0 && mutations}

Loading…
Cancel
Save