0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

resubscribe props if reassigned

This commit is contained in:
Tan Li Hau 2019-10-06 13:07:08 +08:00
parent 9534b27ac5
commit 8c4fd74543
4 changed files with 44 additions and 9 deletions

View File

@ -772,7 +772,7 @@ export default class Component {
invalidate(name, value?) {
const variable = this.var_lookup.get(name);
if (variable && (variable.subscribable && variable.reassigned)) {
if (variable && (variable.subscribable && (variable.reassigned || variable.export_name))) {
return x`${`$$subscribe_${name}`}($$invalidate('${name}', ${value || name}))`;
}

View File

@ -221,16 +221,18 @@ export default function dom(
}
});
component.rewrite_props(({ name, reassigned }) => {
component.rewrite_props(({ name, reassigned, export_name }) => {
const value = `$${name}`;
let insert: string;
if (reassigned || export_name) {
insert = b`${`$$subscribe_${name}`}()`;
} else {
const callback = x`$$value => $$invalidate('${value}', ${value} = $$value)`;
if (reassigned) {
return b`${`$$subscribe_${name}`}()`;
insert = b`@component_subscribe($$self, ${name}, $${callback})`;
}
const callback = x`$$value => $$invalidate('${value}', ${value} = $$value)`;
let insert = b`@component_subscribe($$self, ${name}, $${callback})`;
if (component.compile_options.dev) {
insert = b`@validate_store(${name}, '${name}'); ${insert}`;
}
@ -311,7 +313,7 @@ export default function dom(
const resubscribable_reactive_store_unsubscribers = reactive_stores
.filter(store => {
const variable = component.var_lookup.get(store.name.slice(1));
return variable && variable.reassigned;
return variable && (variable.reassigned || variable.export_name);
})
.map(({ name }) => b`$$self.$$.on_destroy.push(() => ${`$$unsubscribe_${name.slice(1)}`}());`);
@ -353,7 +355,7 @@ export default function dom(
const name = $name.slice(1);
const store = component.var_lookup.get(name);
if (store && store.reassigned) {
if (store && (store.reassigned || store.export_name)) {
const unsubscribe = `$$unsubscribe_${name}`;
const subscribe = `$$subscribe_${name}`;
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => $$invalidate('${$name}', ${$name} = $$value)), ${name})`;

View File

@ -0,0 +1,27 @@
let subscribeCalled = false;
const fakeStore = val => ({
subscribe: cb => {
cb(val);
return {
unsubscribe: () => {
subscribeCalled = true;
},
};
},
});
export default {
props: {
foo: fakeStore(1),
},
html: `
<h1>1</h1>
`,
async test({ assert, component, target }) {
component.foo = fakeStore(5);
return assert.htmlEqual(target.innerHTML, `<h1>5</h1>`);
},
};

View File

@ -0,0 +1,6 @@
<script>
import { writable } from 'svelte/store';
export let foo = writable(0);
</script>
<h1>{$foo}</h1>