pull/992/head
Rich Harris 8 years ago
parent 48ed685fd9
commit 05d63e6ba7

@ -1,22 +0,0 @@
import { assign } from '../../shared/index.js';
interface StateData {
parentNode?: string;
parentNodes?: string;
}
export default class State {
parentNode?: string;
parentNodes?: string;
constructor(data: StateData = {}) {
assign(this, data)
}
child(data?: StateData) {
return new State(assign({}, this, {
parentNode: null,
parentNodes: 'nodes'
}, data));
}
}

@ -42,10 +42,7 @@ export default class Attribute {
this.value = value; this.value = value;
} }
render( render(block: Block) {
block: Block,
state: State
) {
const node = this.parent; const node = this.parent;
const name = this.name; const name = this.name;
@ -151,7 +148,7 @@ export default class Attribute {
name === 'value' && node.name === 'select'; name === 'value' && node.name === 'select';
const last = (shouldCache || isSelectValueAttribute) && block.getUniqueName( const last = (shouldCache || isSelectValueAttribute) && block.getUniqueName(
`${state.parentNode}_${name.replace(/[^a-zA-Z_$]/g, '_')}_value` `${node.var}_${name.replace(/[^a-zA-Z_$]/g, '_')}_value`
); );
if (shouldCache || isSelectValueAttribute) block.addVariable(last); if (shouldCache || isSelectValueAttribute) block.addVariable(last);
@ -161,9 +158,9 @@ export default class Attribute {
if (isLegacyInputType) { if (isLegacyInputType) {
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`@setInputType(${state.parentNode}, ${init});` `@setInputType(${node.var}, ${init});`
); );
updater = `@setInputType(${state.parentNode}, ${shouldCache ? last : value});`; updater = `@setInputType(${node.var}, ${shouldCache ? last : value});`;
} else if (isSelectValueAttribute) { } else if (isSelectValueAttribute) {
// annoying special case // annoying special case
const isMultipleSelect = const isMultipleSelect =
@ -184,8 +181,8 @@ export default class Attribute {
}`; }`;
updater = deindent` updater = deindent`
for (var ${i} = 0; ${i} < ${state.parentNode}.options.length; ${i} += 1) { for (var ${i} = 0; ${i} < ${node.var}.options.length; ${i} += 1) {
var ${option} = ${state.parentNode}.options[${i}]; var ${option} = ${node.var}.options[${i}];
${ifStatement} ${ifStatement}
} }
@ -199,19 +196,19 @@ export default class Attribute {
block.builders.update.addLine(`${last} = ${value};`); block.builders.update.addLine(`${last} = ${value};`);
} else if (propertyName) { } else if (propertyName) {
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`${state.parentNode}.${propertyName} = ${init};` `${node.var}.${propertyName} = ${init};`
); );
updater = `${state.parentNode}.${propertyName} = ${shouldCache || isSelectValueAttribute ? last : value};`; updater = `${node.var}.${propertyName} = ${shouldCache || isSelectValueAttribute ? last : value};`;
} else if (isDataSet) { } else if (isDataSet) {
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`${state.parentNode}.dataset.${camelCaseName} = ${init};` `${node.var}.dataset.${camelCaseName} = ${init};`
); );
updater = `${state.parentNode}.dataset.${camelCaseName} = ${shouldCache || isSelectValueAttribute ? last : value};`; updater = `${node.var}.dataset.${camelCaseName} = ${shouldCache || isSelectValueAttribute ? last : value};`;
} else { } else {
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`${method}(${state.parentNode}, "${name}", ${init});` `${method}(${node.var}, "${name}", ${init});`
); );
updater = `${method}(${state.parentNode}, "${name}", ${shouldCache || isSelectValueAttribute ? last : value});`; updater = `${method}(${node.var}, "${name}", ${shouldCache || isSelectValueAttribute ? last : value});`;
} }
if (allDependencies.size || hasChangeableIndex || isSelectValueAttribute) { if (allDependencies.size || hasChangeableIndex || isSelectValueAttribute) {
@ -240,22 +237,22 @@ export default class Attribute {
: stringify(this.value[0].data); : stringify(this.value[0].data);
const statement = ( const statement = (
isLegacyInputType ? `@setInputType(${state.parentNode}, ${value});` : isLegacyInputType ? `@setInputType(${node.var}, ${value});` :
propertyName ? `${state.parentNode}.${propertyName} = ${value};` : propertyName ? `${node.var}.${propertyName} = ${value};` :
isDataSet ? `${state.parentNode}.dataset.${camelCaseName} = ${value};` : isDataSet ? `${node.var}.dataset.${camelCaseName} = ${value};` :
`${method}(${state.parentNode}, "${name}", ${value});` `${method}(${node.var}, "${name}", ${value});`
); );
block.builders.hydrate.addLine(statement); block.builders.hydrate.addLine(statement);
// special case autofocus. has to be handled in a bit of a weird way // special case autofocus. has to be handled in a bit of a weird way
if (this.value === true && name === 'autofocus') { if (this.value === true && name === 'autofocus') {
block.autofocus = state.parentNode; block.autofocus = node.var;
} }
} }
if (isIndirectlyBoundValue) { if (isIndirectlyBoundValue) {
const updateValue = `${state.parentNode}.value = ${state.parentNode}.__value;`; const updateValue = `${node.var}.value = ${node.var}.__value;`;
block.builders.hydrate.addLine(updateValue); block.builders.hydrate.addLine(updateValue);
if (isDynamic) block.builders.update.addLine(updateValue); if (isDynamic) block.builders.update.addLine(updateValue);

@ -64,7 +64,7 @@ export default class AwaitBlock extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const name = this.var; const name = this.var;
@ -204,10 +204,11 @@ export default class AwaitBlock extends Node {
`); `);
[this.pending, this.then, this.catch].forEach(status => { [this.pending, this.then, this.catch].forEach(status => {
const childState = state.child(); // TODO is this necessary?
status.children.forEach(child => { status.children.forEach(child => {
child.build(status._block, childState); child.build(status._block, {
parentNode: null,
parentNodes: 'nodes'
});
}); });
}); });
} }

@ -21,7 +21,6 @@ export default class Binding extends Node {
munge( munge(
block: Block, block: Block,
state: State,
allUsedContexts: Set<string> allUsedContexts: Set<string>
) { ) {
const node: Element = this.parent; const node: Element = this.parent;

@ -62,7 +62,7 @@ export default class Component extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { generator } = this; const { generator } = this;
generator.hasComponents = true; generator.hasComponents = true;
@ -75,12 +75,11 @@ export default class Component extends Node {
const slots = Array.from(this._slots).map(name => `${name}: @createFragment()`); const slots = Array.from(this._slots).map(name => `${name}: @createFragment()`);
componentInitProperties.push(`slots: { ${slots.join(', ')} }`); componentInitProperties.push(`slots: { ${slots.join(', ')} }`);
const childState = state.child({
parentNode: `${this.var}._slotted.default`
});
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(block, childState); child.build(block, {
parentNode: `${this.var}._slotted.default`,
parentNodes: 'nodes'
});
}); });
} }

@ -104,7 +104,7 @@ export default class EachBlock extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { generator } = this; const { generator } = this;
@ -221,15 +221,19 @@ export default class EachBlock extends Node {
`); `);
} }
const childState = state.child(); // TODO is this necessary? reuse state?
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(this._block, childState); child.build(this._block, {
parentNode: null,
parentNodes: 'nodes'
});
}); });
if (this.else) { if (this.else) {
const childState = state.child(); // TODO is this necessary? reuse state?
this.else.children.forEach((child: Node) => { this.else.children.forEach((child: Node) => {
child.build(this.else._block, childState); child.build(this.else._block, {
parentNode: null,
parentNodes: 'nodes'
});
}); });
} }
} }
@ -238,7 +242,7 @@ export default class EachBlock extends Node {
function keyed( function keyed(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: EachBlock, node: EachBlock,
snippet: string, snippet: string,
{ {
@ -458,7 +462,7 @@ function keyed(
function unkeyed( function unkeyed(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: EachBlock, node: EachBlock,
snippet: string, snippet: string,
{ {

@ -164,7 +164,7 @@ export default class Element extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { generator } = this; const { generator } = this;
@ -173,12 +173,12 @@ export default class Element extends Node {
this.generator.slots.add(slotName); this.generator.slots.add(slotName);
} }
const childState = state.child({ const childState = {
parentNode: this.var, parentNode: this.var,
parentNodes: block.getUniqueName(`${this.var}_nodes`) parentNodes: block.getUniqueName(`${this.var}_nodes`)
}); };
const name = childState.parentNode; const name = this.var;
const allUsedContexts: Set<string> = new Set(); const allUsedContexts: Set<string> = new Set();
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot'); const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
@ -245,10 +245,10 @@ export default class Element extends Node {
}); });
} }
this.addBindings(block, childState, allUsedContexts); this.addBindings(block, allUsedContexts);
this.attributes.filter((a: Attribute) => a.type === 'Attribute').forEach((attribute: Attribute) => { this.attributes.filter((a: Attribute) => a.type === 'Attribute').forEach((attribute: Attribute) => {
attribute.render(block, childState); attribute.render(block);
}); });
// event handlers // event handlers
@ -435,7 +435,6 @@ export default class Element extends Node {
addBindings( addBindings(
block: Block, block: Block,
state: State,
allUsedContexts: Set<string> allUsedContexts: Set<string>
) { ) {
const bindings: Binding[] = this.attributes.filter((a: Binding) => a.type === 'Binding'); const bindings: Binding[] = this.attributes.filter((a: Binding) => a.type === 'Binding');
@ -445,7 +444,7 @@ export default class Element extends Node {
const needsLock = this.name !== 'input' || !/radio|checkbox|range|color/.test(this.getStaticAttributeValue('type')); const needsLock = this.name !== 'input' || !/radio|checkbox|range|color/.test(this.getStaticAttributeValue('type'));
const mungedBindings = bindings.map(binding => binding.munge(block, state, allUsedContexts)); const mungedBindings = bindings.map(binding => binding.munge(block, allUsedContexts));
const lock = mungedBindings.some(binding => binding.needsLock) ? const lock = mungedBindings.some(binding => binding.needsLock) ?
block.getUniqueName(`${this.var}_updating`) : block.getUniqueName(`${this.var}_updating`) :

@ -5,7 +5,6 @@ import State from '../dom/State';
export default class Fragment extends Node { export default class Fragment extends Node {
block: Block; block: Block;
state: State;
children: Node[]; children: Node[];
init() { init() {
@ -34,13 +33,11 @@ export default class Fragment extends Node {
build() { build() {
this.init(); this.init();
const state = new State({
parentNode: null,
parentNodes: 'nodes'
});
this.children.forEach(child => { this.children.forEach(child => {
child.build(this.block, state); child.build(this.block, {
parentNode: null,
parentNodes: 'nodes'
});
}); });
} }
} }

@ -92,7 +92,7 @@ export default class IfBlock extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const name = this.var; const name = this.var;
@ -156,7 +156,7 @@ export default class IfBlock extends Node {
function getBranches( function getBranches(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: Node node: Node
) { ) {
block.contextualise(node.expression); // TODO remove block.contextualise(node.expression); // TODO remove
@ -171,7 +171,7 @@ function getBranches(
}, },
]; ];
visitChildren(generator, block, state, node); visitChildren(generator, block, node);
if (isElseIf(node.else)) { if (isElseIf(node.else)) {
branches.push( branches.push(
@ -187,7 +187,7 @@ function getBranches(
}); });
if (node.else) { if (node.else) {
visitChildren(generator, block, state, node.else); visitChildren(generator, block, node.else);
} }
} }
@ -197,12 +197,13 @@ function getBranches(
function visitChildren( function visitChildren(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State,
node: Node node: Node
) { ) {
const childState = state.child(); // TODO necessary?
node.children.forEach((child: Node) => { node.children.forEach((child: Node) => {
child.build(node._block, childState); child.build(node._block, {
parentNode: null,
parentNodes: 'nodes'
});
}); });
} }
@ -211,7 +212,7 @@ function visitChildren(
function simple( function simple(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: Node, node: Node,
branch, branch,
dynamic, dynamic,
@ -300,7 +301,7 @@ function simple(
function compound( function compound(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: Node, node: Node,
branches, branches,
dynamic, dynamic,
@ -375,7 +376,7 @@ function compound(
function compoundWithOutros( function compoundWithOutros(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: { parentNode: string, parentNodes: string },
node: Node, node: Node,
branches, branches,
dynamic, dynamic,

@ -12,7 +12,7 @@ export default class MustacheTag extends Tag {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { init } = this.renameThisMethod( const { init } = this.renameThisMethod(
block, block,

@ -13,7 +13,7 @@ export default class RawMustacheTag extends Tag {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const name = this.var; const name = this.var;

@ -27,7 +27,7 @@ export default class Slot extends Element {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { generator } = this; const { generator } = this;

@ -35,7 +35,7 @@ export default class Text extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
if (this.shouldSkip) return; if (this.shouldSkip) return;

@ -41,7 +41,7 @@ export default class Window extends Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
const { generator } = this; const { generator } = this;

@ -118,7 +118,7 @@ export default class Node {
build( build(
block: Block, block: Block,
state: State state: { parentNode: string, parentNodes: string }
) { ) {
// implemented by subclasses // implemented by subclasses
} }

Loading…
Cancel
Save