diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 58d7db39ba..20c0d09e24 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -759,7 +759,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}))`; } diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index bd7b99bd8f..ba070beee9 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -221,18 +221,15 @@ export default function dom( } }); - component.rewrite_props(({ name, reassigned }) => { + component.rewrite_props(({ name, reassigned, export_name }) => { const value = `$${name}`; + + const insert = (reassigned || export_name) + ? b`${`$$subscribe_${name}`}()` + : b`@component_subscribe($$self, ${name}, #value => $$invalidate('${value}', ${value} = #value))`; - if (reassigned) { - return b`${`$$subscribe_${name}`}()`; - } - - 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}`; + return b`@validate_store(${name}, '${name}'); ${insert}`; } return insert; @@ -311,7 +308,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 +350,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})`; diff --git a/test/runtime/samples/store-resubscribe-export/_config.js b/test/runtime/samples/store-resubscribe-export/_config.js new file mode 100644 index 0000000000..b6e6f11344 --- /dev/null +++ b/test/runtime/samples/store-resubscribe-export/_config.js @@ -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: ` +