Merge pull request #2235 from sveltejs/gh-2139

Subscribe to global stores
pull/2244/head
Rich Harris 6 years ago committed by GitHub
commit 8620b1f62a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -168,7 +168,7 @@ export default class Component {
this.add_reference(subscribable_name);
const variable = this.var_lookup.get(subscribable_name);
variable.subscribable = true;
if (variable) variable.subscribable = true;
} else {
this.usedNames.add(name);
}

@ -326,7 +326,7 @@ export default function dom(
const reactive_store_subscriptions = reactive_stores
.filter(store => {
const variable = component.var_lookup.get(store.name.slice(1));
return variable.hoistable;
return !variable || variable.hoistable;
})
.map(({ name }) => deindent`
${component.compileOptions.dev && `@validate_store(${name.slice(1)}, '${name.slice(1)}');`}
@ -336,7 +336,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.reassigned;
return variable && variable.reassigned;
})
.map(({ name }) => `$$self.$$.on_destroy.push(() => $$unsubscribe_${name.slice(1)}());`);
@ -370,7 +370,7 @@ export default function dom(
const name = $name.slice(1);
const store = component.var_lookup.get(name);
if (store.reassigned) {
if (store && store.reassigned) {
return `${$name}, $$unsubscribe_${name} = @noop, $$subscribe_${name} = () => { $$unsubscribe_${name}(); $$unsubscribe_${name} = ${name}.subscribe($$value => { ${$name} = $$value; $$invalidate('${$name}', ${$name}); }) }`
}

@ -27,13 +27,14 @@ export default function ssr(
const reactive_stores = component.vars.filter(variable => variable.name[0] === '$' && variable.name[1] !== '$');
const reactive_store_values = reactive_stores
.map(({ name }) => {
const store = component.var_lookup.get(name.slice(1));
if (store.hoistable) return;
const store_name = name.slice(1);
const store = component.var_lookup.get(store_name);
if (store && store.hoistable) return;
const assignment = `${name} = @get_store_value(${store.name});`;
const assignment = `${name} = @get_store_value(${store_name});`;
return component.compileOptions.dev
? `@validate_store(${store.name}, '${store.name}'); ${assignment}`
? `@validate_store(${store_name}, '${store_name}'); ${assignment}`
: assignment;
});
@ -95,10 +96,11 @@ export default function ssr(
const blocks = [
reactive_stores.length > 0 && `let ${reactive_stores
.map(({ name }) => {
const store = component.var_lookup.get(name.slice(1));
if (store.hoistable) {
const store_name = name.slice(1);
const store = component.var_lookup.get(store_name);
if (store && store.hoistable) {
const get_store_value = component.helper('get_store_value');
return `${name} = ${get_store_value}(${store.name})`;
return `${name} = ${get_store_value}(${store_name})`;
}
return name;
})

@ -0,0 +1,3 @@
export default {
error: 'missingGlobal is not defined'
};
Loading…
Cancel
Save