remove specific handling of props from $capture_state

pull/3822/head
rixo 6 years ago
parent 8cdcdc0163
commit 00ef7bd223

@ -164,28 +164,12 @@ export default function dom(
`; `;
} }
// 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)
const is_not_sub = (variable: Var) => variable.name.substr(0, 1) !== '$';
const capturable_vars = component.vars.filter( const capturable_vars = component.vars.filter(
variable => !variable.module && variable.writable && is_not_sub(variable) variable => variable.writable && !(variable.injected || variable.name[0] === '$')
); );
if (uses_props || capturable_vars.length > 0) { if (uses_props || capturable_vars.length > 0) {
capture_state = x`() => ({ ${capturable_vars.map(prop => p`${prop.name}`)} }) `;
const capturable_props = writable_props.filter(is_not_sub);
const local_vars = capturable_vars.filter(variable => !variable.export_name);
const var_names = (variables: Var[]) => variables.map(prop => p`${prop.name}`);
capture_state = x`
({ props: $p = true, local: $l = true } = {}) => ({
...${x`$p && { ${var_names(capturable_props)} }`},
...${x`$l && { ${var_names(local_vars)} }`}
})
`;
inject_state = x` inject_state = x`
${$$props} => { ${$$props} => {

@ -39,7 +39,7 @@ function create_fragment(ctx) {
t4 = text(/*local*/ ctx[2]); t4 = text(/*local*/ ctx[2]);
t5 = space(); t5 = space();
t6 = text(priv); t6 = text(priv);
add_location(p, file, 11, 0, 204); add_location(p, file, 13, 0, 232);
}, },
l: function claim(nodes) { l: function claim(nodes) {
throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
@ -100,10 +100,7 @@ function instance($$self, $$props, $$invalidate) {
if ("alias" in $$props) $$invalidate(0, realName = $$props.alias); if ("alias" in $$props) $$invalidate(0, realName = $$props.alias);
}; };
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ $$self.$capture_state = () => ({ prop, realName, local });
...$p && ({ prop, realName }),
...$l && ({ local })
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("prop" in $$props) $$subscribe_prop($$invalidate(3, prop = $$props.prop)); if ("prop" in $$props) $$subscribe_prop($$invalidate(3, prop = $$props.prop));
@ -111,6 +108,8 @@ function instance($$self, $$props, $$invalidate) {
if ("local" in $$props) $$invalidate(2, local = $$props.local); if ("local" in $$props) $$invalidate(2, local = $$props.local);
}; };
let computed;
$: computed = local * 2;
return [realName, $prop, local, prop]; return [realName, $prop, local, prop];
} }

@ -7,6 +7,8 @@
let local; let local;
const priv = 'priv'; const priv = 'priv';
$: computed = local * 2;
</script> </script>
<!-- NOTE $prop ensures store subscriptions are not part of captured state --> <!-- NOTE $prop ensures store subscriptions are not part of captured state -->
<p>{$prop} {realName} {local} {priv}</p> <p>{$prop} {realName} {local} {priv}</p>

@ -79,7 +79,7 @@ function instance($$self, $$props, $$invalidate) {
if ("name" in $$props) $$invalidate(0, name = $$props.name); if ("name" in $$props) $$invalidate(0, name = $$props.name);
}; };
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ ...$p && ({ name }), ...$l && ({}) }); $$self.$capture_state = () => ({ name });
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("name" in $$props) $$invalidate(0, name = $$props.name); if ("name" in $$props) $$invalidate(0, name = $$props.name);

@ -183,10 +183,7 @@ function instance($$self, $$props, $$invalidate) {
if ("baz" in $$props) $$invalidate(3, baz = $$props.baz); if ("baz" in $$props) $$invalidate(3, baz = $$props.baz);
}; };
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ $$self.$capture_state = () => ({ things, foo, bar, baz });
...$p && ({ things, foo, bar, baz }),
...$l && ({})
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("things" in $$props) $$invalidate(0, things = $$props.things); if ("things" in $$props) $$invalidate(0, things = $$props.things);

@ -173,10 +173,7 @@ function instance($$self, $$props, $$invalidate) {
if ("foo" in $$props) $$invalidate(1, foo = $$props.foo); if ("foo" in $$props) $$invalidate(1, foo = $$props.foo);
}; };
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ $$self.$capture_state = () => ({ things, foo });
...$p && ({ things, foo }),
...$l && ({})
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("things" in $$props) $$invalidate(0, things = $$props.things); if ("things" in $$props) $$invalidate(0, things = $$props.things);

@ -50,11 +50,7 @@ function create_fragment(ctx) {
function instance($$self) { function instance($$self) {
let obj = { x: 5 }; let obj = { x: 5 };
let kobzol = 5; let kobzol = 5;
$$self.$capture_state = () => ({ obj, kobzol });
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({
...$p && ({}),
...$l && ({ obj, kobzol })
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("obj" in $$props) $$invalidate(0, obj = $$props.obj); if ("obj" in $$props) $$invalidate(0, obj = $$props.obj);

@ -76,7 +76,7 @@ function instance($$self, $$props, $$invalidate) {
if ("foo" in $$props) $$invalidate(0, foo = $$props.foo); if ("foo" in $$props) $$invalidate(0, foo = $$props.foo);
}; };
$$self.$capture_state = ({ props: $p = true, local: $l = true } = {}) => ({ ...$p && ({ foo }), ...$l && ({ bar }) }); $$self.$capture_state = () => ({ foo, bar });
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ("foo" in $$props) $$invalidate(0, foo = $$props.foo); if ("foo" in $$props) $$invalidate(0, foo = $$props.foo);

Loading…
Cancel
Save