capture local variables in $capture_state

pull/3822/head
rixo 6 years ago
parent 1273f97808
commit cf3fe22e9b

@ -1,7 +1,7 @@
import { b, x, p } from 'code-red'; import { b, x, p } from 'code-red';
import Component from '../Component'; import Component from '../Component';
import Renderer from './Renderer'; import Renderer from './Renderer';
import { CompileOptions } from '../../interfaces'; import { CompileOptions, Var } from '../../interfaces';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import add_to_set from '../utils/add_to_set'; import add_to_set from '../utils/add_to_set';
import { extract_names } from '../utils/scope'; import { extract_names } from '../utils/scope';
@ -167,27 +167,41 @@ export default function dom(
`; `;
} }
capture_state = (uses_props || writable_props.length > 0) ? x` // we need to filter out store subscriptions ($x) or $inject_state will try to call .set() on them, leading
() => { // to a crash if store is not writable (and probably not intended behaviour to change store value)
return { ${component.vars.filter(prop => prop.writable).map(prop => p`${prop.name}`)} }; const is_not_sub = (variable: Var) => variable.name.substr(0, 1) !== '$';
}
` : x`
() => {
return {};
}
`;
const writable_vars = component.vars.filter(variable => !variable.module && variable.writable); const capturable_vars = component.vars.filter(
inject_state = (uses_props || writable_vars.length > 0) ? x` variable => !variable.module && variable.writable && is_not_sub(variable)
${$$props} => { );
${uses_props && component.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), $$new_props)`)}
${writable_vars.map(prop => b` if (uses_props || capturable_vars.length > 0) {
if ('${prop.name}' in $$props) ${component.invalidate(prop.name, x`${prop.name} = ${$$props}.${prop.name}`)};
`)} const capturable_props = writable_props.filter(is_not_sub);
}
` : x` const local_vars = capturable_vars.filter(variable => !variable.export_name);
${$$props} => {}
`; const var_names = (variables: Var[]) => variables.map(prop => p`${prop.name}`);
capture_state = x`
({ props: $props = true, local: $local = true } = {}) => ({
...${x`$props && { ${var_names(capturable_props)} }`},
...${x`$local && { ${var_names(local_vars)} }`}
})
`;
inject_state = x`
${$$props} => {
${uses_props && component.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), $$new_props)`)}
${capturable_vars.map(prop => b`
if ('${prop.name}' in $$props) ${component.invalidate(prop.name, x`${prop.name} = ${$$props}.${prop.name}`)};
`)}
}
`;
} else {
capture_state = x`() => ({})`;
inject_state = x`() => {}`;
}
} }
// instrument assignments // instrument assignments

@ -78,9 +78,10 @@ function instance($$self, $$props, $$invalidate) {
if ("name" in $$props) $$invalidate("name", name = $$props.name); if ("name" in $$props) $$invalidate("name", name = $$props.name);
}; };
$$self.$capture_state = () => { $$self.$capture_state = ({ props: $props = true, local: $local = true } = {}) => ({
return { name }; ...$props && ({ name }),
}; ...$local && ({})
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("name" in $$props) $$invalidate("name", name = $$props.name); if ("name" in $$props) $$invalidate("name", name = $$props.name);

@ -175,9 +175,10 @@ function instance($$self, $$props, $$invalidate) {
if ("baz" in $$props) $$invalidate("baz", baz = $$props.baz); if ("baz" in $$props) $$invalidate("baz", baz = $$props.baz);
}; };
$$self.$capture_state = () => { $$self.$capture_state = ({ props: $props = true, local: $local = true } = {}) => ({
return { things, foo, bar, baz }; ...$props && ({ things, foo, bar, baz }),
}; ...$local && ({})
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("things" in $$props) $$invalidate("things", things = $$props.things); if ("things" in $$props) $$invalidate("things", things = $$props.things);

@ -171,9 +171,10 @@ function instance($$self, $$props, $$invalidate) {
if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo); if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo);
}; };
$$self.$capture_state = () => { $$self.$capture_state = ({ props: $props = true, local: $local = true } = {}) => ({
return { things, foo }; ...$props && ({ things, foo }),
}; ...$local && ({})
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("things" in $$props) $$invalidate("things", things = $$props.things); if ("things" in $$props) $$invalidate("things", things = $$props.things);

@ -49,9 +49,10 @@ let kobzol = 5;
function instance($$self) { function instance($$self) {
let obj = { x: 5 }; let obj = { x: 5 };
$$self.$capture_state = () => { $$self.$capture_state = ({ props: $props = true, local: $local = true } = {}) => ({
return {}; ...$props && ({}),
}; ...$local && ({ obj, kobzol })
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("obj" in $$props) $$invalidate("obj", obj = $$props.obj); if ("obj" in $$props) $$invalidate("obj", obj = $$props.obj);

@ -75,9 +75,10 @@ function instance($$self, $$props, $$invalidate) {
if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo); if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo);
}; };
$$self.$capture_state = () => { $$self.$capture_state = ({ props: $props = true, local: $local = true } = {}) => ({
return { foo, bar }; ...$props && ({ foo }),
}; ...$local && ({ bar })
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo); if ("foo" in $$props) $$invalidate("foo", foo = $$props.foo);

Loading…
Cancel
Save