remove confusing state object from init

pull/992/head
Rich Harris 8 years ago
parent f4888dd690
commit 7f77a10533

@ -19,7 +19,6 @@ export default class AwaitBlock extends Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -49,9 +48,7 @@ export default class AwaitBlock extends Node {
contexts
});
child._state = state.child();
child.initChildren(child._block, child._state, stripWhitespace, nextSibling);
child.initChildren(child._block, stripWhitespace, nextSibling);
this.generator.blocks.push(child._block);
if (child._block.dependencies.size > 0) {
@ -207,8 +204,10 @@ export default class AwaitBlock extends Node {
`);
[this.pending, this.then, this.catch].forEach(status => {
const childState = state.child(); // TODO is this necessary?
status.children.forEach(child => {
child.build(status._block, status._state);
child.build(status._block, childState);
});
});
}

@ -19,7 +19,6 @@ export default class Component extends Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -52,15 +51,11 @@ export default class Component extends Node {
).toLowerCase()
);
this._state = state.child({
parentNode: `${this.var}._slotted.default`
});
if (this.children.length) {
this._slots = new Set(['default']);
this.children.forEach(child => {
child.init(block, state, stripWhitespace, nextSibling);
child.init(block, stripWhitespace, nextSibling);
});
}
}
@ -80,8 +75,12 @@ export default class Component extends Node {
const slots = Array.from(this._slots).map(name => `${name}: @createFragment()`);
componentInitProperties.push(`slots: { ${slots.join(', ')} }`);
const childState = state.child({
parentNode: `${this.var}._slotted.default`
});
this.children.forEach((child: Node) => {
child.build(block, this._state);
child.build(block, childState);
});
}

@ -10,7 +10,6 @@ export default class EachBlock extends Node {
type: 'EachBlock';
_block: Block;
_state: State;
expression: Node;
iterations: string;
@ -24,7 +23,6 @@ export default class EachBlock extends Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -83,10 +81,8 @@ export default class EachBlock extends Node {
params: block.params.concat(listName, context, indexName),
});
this._state = state.child();
this.generator.blocks.push(this._block);
this.initChildren(this._block, this._state, stripWhitespace, nextSibling);
this.initChildren(this._block, stripWhitespace, nextSibling);
block.addDependencies(this._block.dependencies);
this._block.hasUpdateMethod = this._block.dependencies.size > 0;
@ -96,12 +92,9 @@ export default class EachBlock extends Node {
name: this.generator.getUniqueName(`${this._block.name}_else`),
});
this.else._state = state.child();
this.generator.blocks.push(this.else._block);
this.else.initChildren(
this.else._block,
this.else._state,
stripWhitespace,
nextSibling
);
@ -228,13 +221,15 @@ export default class EachBlock extends Node {
`);
}
const childState = state.child(); // TODO is this necessary? reuse state?
this.children.forEach((child: Node) => {
child.build(this._block, this._state);
child.build(this._block, childState);
});
if (this.else) {
const childState = state.child(); // TODO is this necessary? reuse state?
this.else.children.forEach((child: Node) => {
child.build(this.else._block, this.else._state);
child.build(this.else._block, childState);
});
}
}
@ -269,7 +264,7 @@ function keyed(
if (node.children[0] && node.children[0].type === 'Element' && !generator.components.has(node.children[0].name)) {
// TODO or text/tag/raw
node._block.first = node.children[0]._state.parentNode; // TODO this is highly confusing
node._block.first = node.children[0].var; // TODO this is highly confusing
} else {
node._block.first = node._block.getUniqueName('first');
node._block.addElement(

@ -26,7 +26,6 @@ export default class Element extends Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -34,8 +33,19 @@ export default class Element extends Node {
this.cannotUseInnerHTML();
}
const parentElement = this.findNearest('Element');
this.namespace = this.name === 'svg' ?
namespaces.svg :
parentElement.namespace;
this.attributes.forEach(attribute => {
if (attribute.type === 'Attribute' && attribute.value !== true) {
// special case — xmlns
if (attribute.name === 'xmlns') {
// TODO this attribute must be static enforce at compile time
this.namespace = attribute.value[0].data;
}
attribute.value.forEach((chunk: Node) => {
if (chunk.type !== 'Text') {
if (this.parent) this.parent.cannotUseInnerHTML();
@ -46,14 +56,18 @@ export default class Element extends Node {
// special case — <option value='{{foo}}'> — see below
if (
this.name === 'option' &&
attribute.name === 'value' &&
state.selectBindingDependencies
attribute.name === 'value'
) {
state.selectBindingDependencies.forEach(prop => {
dependencies.forEach((dependency: string) => {
this.generator.indirectDependencies.get(prop).add(dependency);
let select = this.parent;
while (select && select.type !== 'Element' || select.name !== 'select') select = select.parent;
if (select && select.selectBindingDependencies) {
select.selectBindingDependencies.forEach(prop => {
dependencies.forEach((dependency: string) => {
this.generator.indirectDependencies.get(prop).add(dependency);
});
});
});
}
}
}
});
@ -121,12 +135,12 @@ export default class Element extends Node {
if (binding) {
// TODO does this also apply to e.g. `<input type='checkbox' bind:group='foo'>`?
const dependencies = binding.metadata.dependencies;
state.selectBindingDependencies = dependencies;
this.selectBindingDependencies = dependencies;
dependencies.forEach((prop: string) => {
this.generator.indirectDependencies.set(prop, new Set());
});
} else {
state.selectBindingDependencies = null;
this.selectBindingDependencies = null;
}
}
@ -143,21 +157,11 @@ export default class Element extends Node {
this.name.replace(/[^a-zA-Z0-9_$]/g, '_')
);
this._state = state.child({
parentNode: this.var,
parentNodes: block.getUniqueName(`${this.var}_nodes`),
parentNodeName: this.name,
namespace: this.name === 'svg'
? 'http://www.w3.org/2000/svg'
: state.namespace,
allUsedContexts: [],
});
this.generator.stylesheet.apply(this);
if (this.children.length) {
if (this.name === 'pre' || this.name === 'textarea') stripWhitespace = false;
this.initChildren(block, this._state, stripWhitespace, nextSibling);
this.initChildren(block, stripWhitespace, nextSibling);
}
}
@ -172,7 +176,16 @@ export default class Element extends Node {
this.generator.slots.add(slotName);
}
const childState = this._state;
const childState = state.child({
parentNode: this.var,
parentNodes: block.getUniqueName(`${this.var}_nodes`),
parentNodeName: this.name,
namespace: this.name === 'svg'
? 'http://www.w3.org/2000/svg'
: state.namespace,
allUsedContexts: [],
});
const name = childState.parentNode;
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');

@ -5,7 +5,5 @@ import State from '../dom/State';
export default class ElseBlock extends Node {
type: 'ElseBlock';
children: Node[];
_block: Block;
_state: State;
}

@ -25,14 +25,8 @@ export default class Fragment extends Node {
dependencies: new Set(),
});
this.state = new State({
namespace: this.generator.namespace,
parentNode: null,
parentNodes: 'nodes'
});
this.generator.blocks.push(this.block);
this.initChildren(this.block, this.state, true, null);
this.initChildren(this.block, true, null);
this.block.hasUpdateMethod = true;
}
@ -40,8 +34,14 @@ export default class Fragment extends Node {
build() {
this.init();
const state = new State({
namespace: this.generator.namespace,
parentNode: null,
parentNodes: 'nodes'
});
this.children.forEach(child => {
child.build(this.block, this.state);
child.build(this.block, state);
});
}
}

@ -21,11 +21,9 @@ export default class IfBlock extends Node {
else: ElseBlock;
_block: Block;
_state: State;
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -48,10 +46,8 @@ export default class IfBlock extends Node {
name: generator.getUniqueName(`create_if_block`),
});
node._state = state.child();
blocks.push(node._block);
node.initChildren(node._block, node._state, stripWhitespace, nextSibling);
node.initChildren(node._block, stripWhitespace, nextSibling);
if (node._block.dependencies.size > 0) {
dynamic = true;
@ -69,12 +65,9 @@ export default class IfBlock extends Node {
name: generator.getUniqueName(`create_if_block`),
});
node.else._state = state.child();
blocks.push(node.else._block);
node.else.initChildren(
node.else._block,
node.else._state,
stripWhitespace,
nextSibling
);
@ -207,8 +200,9 @@ function visitChildren(
state: State,
node: Node
) {
const childState = state.child(); // TODO necessary?
node.children.forEach((child: Node) => {
child.build(node._block, node._state);
child.build(node._block, childState);
});
}

@ -4,6 +4,5 @@ import State from '../dom/State';
export default class PendingBlock extends Node {
_block: Block;
_state: State;
children: Node[];
}

@ -13,7 +13,6 @@ export default class Slot extends Element {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -21,18 +20,8 @@ export default class Slot extends Element {
this.var = block.getUniqueName('slot');
this._state = state.child({
parentNode: this.var,
parentNodes: block.getUniqueName(`${this.var}_nodes`),
parentNodeName: this.name,
namespace: this.name === 'svg'
? 'http://www.w3.org/2000/svg'
: state.namespace,
allUsedContexts: [],
});
if (this.children.length) {
this.initChildren(block, this._state, stripWhitespace, nextSibling);
this.initChildren(block, stripWhitespace, nextSibling);
}
}
@ -68,6 +57,16 @@ export default class Slot extends Element {
block.builders.unmount.pushCondition(`!${content_name}`);
block.builders.destroy.pushCondition(`!${content_name}`);
// const childState = state.child({
// parentNode: this.var,
// parentNodes: block.getUniqueName(`${this.var}_nodes`),
// parentNodeName: this.name,
// namespace: this.name === 'svg'
// ? 'http://www.w3.org/2000/svg'
// : state.namespace,
// allUsedContexts: [],
// });
this.children.forEach((child: Node) => {
child.build(block, state);
});

@ -22,8 +22,10 @@ export default class Text extends Node {
data: string;
shouldSkip: boolean;
init(block: Block, state: State) {
if (!/\S/.test(this.data) && (state.namespace || elementsWithoutText.has(state.parentNodeName))) {
init(block: Block) {
const parentElement = this.findNearest('Element');
if (!/\S/.test(this.data) && parentElement && (parentElement.namespace || elementsWithoutText.has(parentElement.name))) {
this.shouldSkip = true;
return;
}

@ -33,7 +33,6 @@ export default class Window extends Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {

@ -38,7 +38,6 @@ export default class Node {
init(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -47,7 +46,6 @@ export default class Node {
initChildren(
block: Block,
state: State,
stripWhitespace: boolean,
nextSibling: Node
) {
@ -87,7 +85,7 @@ export default class Node {
cleaned.forEach((child: Node, i: number) => {
child.canUseInnerHTML = !this.generator.hydratable;
child.init(block, state, stripWhitespace, cleaned[i + 1] || nextSibling);
child.init(block, stripWhitespace, cleaned[i + 1] || nextSibling);
if (child.shouldSkip) return;
@ -140,7 +138,13 @@ export default class Node {
false;
}
nearestComponent() {
if (this.parent) return this.parent.nearestComponent();
nearestComponent() { // TODO remove this method
return this.findNearest('Component');
// if (this.parent) return this.parent.nearestComponent();
}
findNearest(type: string) {
if (this.type === type) return this;
if (this.parent) return this.parent.findNearest(type);
}
}
Loading…
Cancel
Save