diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 68947da456..e7770bccfe 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -182,22 +182,10 @@ export default class Block { this.builders.mount.addLine(`${this.autofocus}.focus();`); } - // TODO `this.contexts` is possibly redundant post-#1122 - const initializers = []; - - 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}` - ); - + if (this.maintainContext) { + this.builders.update.addLineAtStart(`ctx = _ctx;`); this.hasUpdateMethod = true; - }); + } // minor hack – we need to ensure that any {{{triples}}} are detached first this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString()); @@ -258,11 +246,11 @@ export default class Block { } if (this.hasUpdateMethod) { - if (this.builders.update.isEmpty() && initializers.length === 0) { + if (this.builders.update.isEmpty()) { properties.addBlock(`p: @noop,`); } else { properties.addBlock(deindent` - p: function update(changed, ctx) { + p: function update(changed, ${this.maintainContext ? '_ctx' : 'ctx'}) { ${this.builders.update} }, `); @@ -334,8 +322,6 @@ export default class Block { return deindent` ${this.comment && `// ${escape(this.comment)}`} function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, ctx) { - ${initializers.length > 0 && - `var ${initializers.join(', ')};`} ${this.variables.size > 0 && `var ${Array.from(this.variables.keys()) .map(key => { diff --git a/src/generators/nodes/Binding.ts b/src/generators/nodes/Binding.ts index 43db14e270..b377317936 100644 --- a/src/generators/nodes/Binding.ts +++ b/src/generators/nodes/Binding.ts @@ -198,8 +198,8 @@ function getEventHandler( ? getTailSnippet(binding.value.node) : ''; - const list = `context.${block.listNames.get(name)}`; - const index = `context.${block.indexNames.get(name)}`; + const list = `ctx.${block.listNames.get(name)}`; + const index = `ctx.${block.indexNames.get(name)}`; return { usesContext: true, diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 1858ddf727..cdcaabb7b1 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -301,7 +301,7 @@ export default class Element extends Node { } let hasHoistedEventHandlerOrBinding = ( - (this.hasAncestor('EachBlock') && this.bindings.length > 0) || + //(this.hasAncestor('EachBlock') && this.bindings.length > 0) || this.handlers.some(handler => handler.shouldHoist) ); let eventHandlerOrBindingUsesComponent; @@ -334,6 +334,10 @@ export default class Element extends Node { ${name}._svelte = { ${initialProps.join(', ')} }; `); } + } else { + if (eventHandlerOrBindingUsesContext) { + block.maintainContext = true; + } } this.addBindings(block, allUsedContexts); @@ -457,8 +461,6 @@ export default class Element extends Node { cancelAnimationFrame(${animation_frame}); if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});` } - ${usesContext && `var context = ${this.var}._svelte;`} - ${usesState && `var ctx = #component.get();`} ${usesStore && `var $ = #component.store.get();`} ${needsLock && `${lock} = true;`} ${mutations.length > 0 && mutations}