tidy up a bit

pull/772/head
Rich Harris 8 years ago
parent 8818357fc4
commit ee5a4b3d0b

@ -153,13 +153,44 @@ export default function visitComponent(
const statements: string[] = [];
let name_updating: string;
let name_initial_data: string;
let _beforecreate: string = null;
let bindings = [];
if (
local.staticAttributes.length ||
local.dynamicAttributes.length ||
local.bindings.length
) {
const initialProps = local.staticAttributes
.concat(local.dynamicAttributes)
.map(attribute => `${attribute.name}: ${attribute.value}`);
const initialPropString = stringifyProps(initialProps);
const updates: string[] = [];
local.dynamicAttributes.forEach(attribute => {
if (attribute.dependencies.length) {
updates.push(deindent`
if ( ${attribute.dependencies
.map(dependency => `changed.${dependency}`)
.join(' || ')} ) ${name}_changes.${attribute.name} = ${attribute.value};
`);
}
else {
// TODO this is an odd situation to encounter I *think* it should only happen with
// each block indices, in which case it may be possible to optimise this
updates.push(`${name}_changes.${attribute.name} = ${attribute.value};`);
}
});
if (local.bindings.length) {
name_updating = block.alias(`${name}_updating`);
name_initial_data = block.getUniqueName(`${name}_initial_data`);
block.addVariable(name_updating, '{}');
statements.push(`var ${name_initial_data} = ${initialPropString};`);
bindings = local.bindings.map(binding => {
let setParentFromChild;
@ -219,21 +250,6 @@ export default function visitComponent(
`
}
});
}
if (
local.staticAttributes.length ||
local.dynamicAttributes.length ||
local.bindings.length
) {
const initialProps = local.staticAttributes
.concat(local.dynamicAttributes)
.map(attribute => `${attribute.name}: ${attribute.value}`);
const initialPropString = stringifyProps(initialProps);
if (local.bindings.length) {
statements.push(`var ${name_initial_data} = ${initialPropString};`);
bindings.forEach(binding => {
statements.push(binding.init);
@ -250,25 +266,12 @@ export default function visitComponent(
${name_updating} = {};
}
`);
} else if (initialProps.length) {
componentInitProperties.push(`data: ${initialPropString}`);
}
}
const expression = node.name === ':Self'
? generator.name
: generator.importedComponents.get(node.name) ||
`@template.components.${node.name}`;
local.create.addBlockAtStart(deindent`
${statements.join('\n')}
var ${name} = new ${expression}({
${componentInitProperties.join(',\n')}
bindings.forEach(binding => {
if (binding.update) updates.push(binding.update);
});
`);
if (bindings.length) {
local.create.addBlock(deindent`
_beforecreate = deindent`
#component._root._beforecreate.push(function () {
var state = #component.get(), childState = ${name}.get(), newState = {};
if (!childState) return;
@ -277,31 +280,10 @@ export default function visitComponent(
#component._set(newState);
${name_updating} = {};
});
`);
}
if (local.dynamicAttributes.length || local.bindings.length) {
const updates: string[] = [];
local.dynamicAttributes.forEach(attribute => {
if (attribute.dependencies.length) {
updates.push(deindent`
if ( ${attribute.dependencies
.map(dependency => `changed.${dependency}`)
.join(' || ')} ) ${name}_changes.${attribute.name} = ${attribute.value};
`);
}
else {
// TODO this is an odd situation to encounter I *think* it should only happen with
// each block indices, in which case it may be possible to optimise this
updates.push(`${name}_changes.${attribute.name} = ${attribute.value};`);
`;
} else if (initialProps.length) {
componentInitProperties.push(`data: ${initialPropString}`);
}
});
bindings.forEach(binding => {
if (binding.update) updates.push(binding.update);
});
local.update.addBlock(deindent`
var ${name}_changes = {};
@ -311,6 +293,20 @@ export default function visitComponent(
`);
}
const expression = node.name === ':Self'
? generator.name
: generator.importedComponents.get(node.name) ||
`@template.components.${node.name}`;
local.create.addBlockAtStart(deindent`
${statements.join('\n')}
var ${name} = new ${expression}({
${componentInitProperties.join(',\n')}
});
${_beforecreate}
`);
if (isTopLevel)
block.builders.unmount.addLine(`${name}._fragment.unmount();`);
block.builders.destroy.addLine(`${name}.destroy( false );`);

Loading…
Cancel
Save