|
|
@ -137,27 +137,21 @@ export default class Component extends Node {
|
|
|
|
block.addVariable(name_updating, '{}');
|
|
|
|
block.addVariable(name_updating, '{}');
|
|
|
|
statements.push(`var ${name_initial_data} = ${initialPropString};`);
|
|
|
|
statements.push(`var ${name_initial_data} = ${initialPropString};`);
|
|
|
|
|
|
|
|
|
|
|
|
const setParentFromChildOnChange = new CodeBuilder();
|
|
|
|
let hasLocalBindings = false;
|
|
|
|
const setParentFromChildOnInit = new CodeBuilder();
|
|
|
|
let hasStoreBindings = false;
|
|
|
|
|
|
|
|
|
|
|
|
const setStoreFromChildOnChange = new CodeBuilder();
|
|
|
|
const builder = new CodeBuilder();
|
|
|
|
const setStoreFromChildOnInit = new CodeBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bindings.forEach((binding: Binding) => {
|
|
|
|
bindings.forEach((binding: Binding) => {
|
|
|
|
let { name: key } = getObject(binding.value);
|
|
|
|
let { name: key } = getObject(binding.value);
|
|
|
|
|
|
|
|
|
|
|
|
const isStoreProp = generator.options.store && key[0] === '$';
|
|
|
|
|
|
|
|
if (isStoreProp) key = key.slice(1);
|
|
|
|
|
|
|
|
const newState = isStoreProp ? 'newStoreState' : 'newState';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binding.contexts.forEach(context => {
|
|
|
|
binding.contexts.forEach(context => {
|
|
|
|
allContexts.add(context);
|
|
|
|
allContexts.add(context);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let setFromChild;
|
|
|
|
let setFromChild;
|
|
|
|
|
|
|
|
|
|
|
|
if (!isStoreProp && block.contexts.has(key)) {
|
|
|
|
if (block.contexts.has(key)) {
|
|
|
|
const prop = binding.dependencies[0];
|
|
|
|
|
|
|
|
const computed = isComputed(binding.value);
|
|
|
|
const computed = isComputed(binding.value);
|
|
|
|
const tail = binding.value.type === 'MemberExpression' ? getTailSnippet(binding.value) : '';
|
|
|
|
const tail = binding.value.type === 'MemberExpression' ? getTailSnippet(binding.value) : '';
|
|
|
|
|
|
|
|
|
|
|
@ -167,20 +161,38 @@ export default class Component extends Node {
|
|
|
|
list[index]${tail} = childState.${binding.name};
|
|
|
|
list[index]${tail} = childState.${binding.name};
|
|
|
|
|
|
|
|
|
|
|
|
${binding.dependencies
|
|
|
|
${binding.dependencies
|
|
|
|
.map((prop: string) => `${newState}.${prop} = state.${prop};`)
|
|
|
|
.map((name: string) => {
|
|
|
|
|
|
|
|
const isStoreProp = generator.options.store && name[0] === '$';
|
|
|
|
|
|
|
|
const prop = isStoreProp ? name.slice(1) : name;
|
|
|
|
|
|
|
|
const newState = isStoreProp ? 'newStoreState' : 'newState';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isStoreProp) hasStoreBindings = true;
|
|
|
|
|
|
|
|
else hasLocalBindings = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return `${newState}.${prop} = state.${name};`;
|
|
|
|
|
|
|
|
})
|
|
|
|
.join('\n')}
|
|
|
|
.join('\n')}
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (binding.value.type === 'MemberExpression') {
|
|
|
|
else {
|
|
|
|
|
|
|
|
const isStoreProp = generator.options.store && key[0] === '$';
|
|
|
|
|
|
|
|
const prop = isStoreProp ? key.slice(1) : key;
|
|
|
|
|
|
|
|
const newState = isStoreProp ? 'newStoreState' : 'newState';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isStoreProp) hasStoreBindings = true;
|
|
|
|
|
|
|
|
else hasLocalBindings = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (binding.value.type === 'MemberExpression') {
|
|
|
|
setFromChild = deindent`
|
|
|
|
setFromChild = deindent`
|
|
|
|
${binding.snippet} = childState.${binding.name};
|
|
|
|
${binding.snippet} = childState.${binding.name};
|
|
|
|
${binding.dependencies.map((prop: string) => `${newState}.${prop} = state.${prop};`).join('\n')}
|
|
|
|
${newState}.${prop} = state.${key};
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
setFromChild = `${newState}.${key} = childState.${binding.name};`;
|
|
|
|
setFromChild = `${newState}.${prop} = childState.${binding.name};`;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
statements.push(deindent`
|
|
|
|
statements.push(deindent`
|
|
|
@ -190,16 +202,11 @@ export default class Component extends Node {
|
|
|
|
}`
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
(isStoreProp ? setStoreFromChildOnChange : setParentFromChildOnChange).addConditional(
|
|
|
|
builder.addConditional(
|
|
|
|
`!${name_updating}.${binding.name} && changed.${binding.name}`,
|
|
|
|
`!${name_updating}.${binding.name} && changed.${binding.name}`,
|
|
|
|
setFromChild
|
|
|
|
setFromChild
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
(isStoreProp ? setStoreFromChildOnInit : setParentFromChildOnInit).addConditional(
|
|
|
|
|
|
|
|
`!${name_updating}.${binding.name}`,
|
|
|
|
|
|
|
|
setFromChild
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO could binding.dependencies.length ever be 0?
|
|
|
|
// TODO could binding.dependencies.length ever be 0?
|
|
|
|
if (binding.dependencies.length) {
|
|
|
|
if (binding.dependencies.length) {
|
|
|
|
updates.push(deindent`
|
|
|
|
updates.push(deindent`
|
|
|
@ -215,44 +222,23 @@ export default class Component extends Node {
|
|
|
|
|
|
|
|
|
|
|
|
const initialisers = [
|
|
|
|
const initialisers = [
|
|
|
|
'state = #component.get()',
|
|
|
|
'state = #component.get()',
|
|
|
|
!setParentFromChildOnChange.isEmpty() && 'newState = {}',
|
|
|
|
hasLocalBindings && 'newState = {}',
|
|
|
|
!setStoreFromChildOnChange.isEmpty() && 'newStoreState = {}',
|
|
|
|
hasStoreBindings && 'newStoreState = {}',
|
|
|
|
].filter(Boolean).join(', ');
|
|
|
|
].filter(Boolean).join(', ');
|
|
|
|
|
|
|
|
|
|
|
|
componentInitProperties.push(deindent`
|
|
|
|
componentInitProperties.push(deindent`
|
|
|
|
_bind: function(changed, childState) {
|
|
|
|
_bind: function(changed, childState) {
|
|
|
|
var ${initialisers};
|
|
|
|
var ${initialisers};
|
|
|
|
${!setStoreFromChildOnChange.isEmpty() && deindent`
|
|
|
|
${builder}
|
|
|
|
${setStoreFromChildOnChange}
|
|
|
|
${hasStoreBindings && `#component.store.set(newStoreState);`}
|
|
|
|
${name_updating} = @assign({}, changed);
|
|
|
|
${hasLocalBindings && `#component._set(newState);`}
|
|
|
|
#component.store.set(newStoreState);
|
|
|
|
|
|
|
|
`}
|
|
|
|
|
|
|
|
${!setParentFromChildOnChange.isEmpty() && deindent`
|
|
|
|
|
|
|
|
${setParentFromChildOnChange}
|
|
|
|
|
|
|
|
${name_updating} = @assign({}, changed);
|
|
|
|
|
|
|
|
#component._set(newState);
|
|
|
|
|
|
|
|
`}
|
|
|
|
|
|
|
|
${name_updating} = {};
|
|
|
|
${name_updating} = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO can `!childState` ever be true?
|
|
|
|
|
|
|
|
beforecreate = deindent`
|
|
|
|
beforecreate = deindent`
|
|
|
|
#component.root._beforecreate.push(function() {
|
|
|
|
#component.root._beforecreate.push(function() {
|
|
|
|
var childState = ${name}.get(), ${initialisers};
|
|
|
|
${name}._bind({ ${bindings.map(b => `${b.name}: 1`).join(', ')} }, ${name}.get());
|
|
|
|
if (!childState) return;
|
|
|
|
|
|
|
|
${setParentFromChildOnInit}
|
|
|
|
|
|
|
|
${!setStoreFromChildOnInit.isEmpty() && deindent`
|
|
|
|
|
|
|
|
${setStoreFromChildOnInit}
|
|
|
|
|
|
|
|
${name_updating} = { ${bindings.map((binding: Binding) => `${binding.name}: true`).join(', ')} };
|
|
|
|
|
|
|
|
#component.store.set(newStoreState);
|
|
|
|
|
|
|
|
`}
|
|
|
|
|
|
|
|
${!setParentFromChildOnInit.isEmpty() && deindent`
|
|
|
|
|
|
|
|
${setParentFromChildOnInit}
|
|
|
|
|
|
|
|
${name_updating} = { ${bindings.map((binding: Binding) => `${binding.name}: true`).join(', ')} };
|
|
|
|
|
|
|
|
#component._set(newState);
|
|
|
|
|
|
|
|
`}
|
|
|
|
|
|
|
|
${name_updating} = {};
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
} else if (initialProps.length) {
|
|
|
|
} else if (initialProps.length) {
|
|
|
|