pull/992/head
Rich Harris 8 years ago
parent e30dc8e3fa
commit 8eacc2597f

@ -4,14 +4,12 @@ interface StateData {
parentNode?: string; parentNode?: string;
parentNodes?: string; parentNodes?: string;
allUsedContexts?: string[]; allUsedContexts?: string[];
usesComponent?: boolean;
} }
export default class State { export default class State {
parentNode?: string; parentNode?: string;
parentNodes?: string; parentNodes?: string;
allUsedContexts?: string[]; allUsedContexts?: string[];
usesComponent?: boolean;
constructor(data: StateData = {}) { constructor(data: StateData = {}) {
assign(this, data) assign(this, data)

@ -255,6 +255,8 @@ export default class Element extends Node {
}); });
// event handlers // event handlers
let eventHandlerUsesComponent = false;
this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => { this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => {
const isCustomEvent = generator.events.has(attribute.name); const isCustomEvent = generator.events.has(attribute.name);
const shouldHoist = !isCustomEvent && this.hasAncestor('EachBlock'); const shouldHoist = !isCustomEvent && this.hasAncestor('EachBlock');
@ -273,7 +275,7 @@ export default class Element extends Node {
attribute.expression.start, attribute.expression.start,
`${block.alias('component')}.` `${block.alias('component')}.`
); );
if (shouldHoist) childState.usesComponent = true; // this feels a bit hacky but it works! if (shouldHoist) eventHandlerUsesComponent = true; // this feels a bit hacky but it works!
} }
attribute.expression.arguments.forEach((arg: Node) => { attribute.expression.arguments.forEach((arg: Node) => {
@ -290,7 +292,7 @@ export default class Element extends Node {
const ctx = context || 'this'; const ctx = context || 'this';
const declarations = usedContexts.map(name => { const declarations = usedContexts.map(name => {
if (name === 'state') { if (name === 'state') {
if (shouldHoist) childState.usesComponent = true; if (shouldHoist) eventHandlerUsesComponent = true;
return `var state = ${block.alias('component')}.get();`; return `var state = ${block.alias('component')}.get();`;
} }
@ -309,7 +311,7 @@ export default class Element extends Node {
// create the handler body // create the handler body
const handlerBody = deindent` const handlerBody = deindent`
${childState.usesComponent && ${eventHandlerUsesComponent &&
`var ${block.alias('component')} = ${ctx}._svelte.component;`} `var ${block.alias('component')} = ${ctx}._svelte.component;`}
${declarations} ${declarations}
${attribute.expression ? ${attribute.expression ?
@ -369,11 +371,11 @@ export default class Element extends Node {
this.addTransitions(block); this.addTransitions(block);
if (childState.allUsedContexts.length || childState.usesComponent) { if (childState.allUsedContexts.length || eventHandlerUsesComponent) {
const initialProps: string[] = []; const initialProps: string[] = [];
const updates: string[] = []; const updates: string[] = [];
if (childState.usesComponent) { if (eventHandlerUsesComponent) {
initialProps.push(`component: #component`); initialProps.push(`component: #component`);
} }

Loading…
Cancel
Save