Skip to content

Commit 8620b1f

Browse files
authored
Merge pull request #2235 from sveltejs/gh-2139
Subscribe to global stores
2 parents 362e186 + b57c724 commit 8620b1f

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

src/compile/Component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export default class Component {
168168
this.add_reference(subscribable_name);
169169

170170
const variable = this.var_lookup.get(subscribable_name);
171-
variable.subscribable = true;
171+
if (variable) variable.subscribable = true;
172172
} else {
173173
this.usedNames.add(name);
174174
}

src/compile/render-dom/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export default function dom(
326326
const reactive_store_subscriptions = reactive_stores
327327
.filter(store => {
328328
const variable = component.var_lookup.get(store.name.slice(1));
329-
return variable.hoistable;
329+
return !variable || variable.hoistable;
330330
})
331331
.map(({ name }) => deindent`
332332
${component.compileOptions.dev && `@validate_store(${name.slice(1)}, '${name.slice(1)}');`}
@@ -336,7 +336,7 @@ export default function dom(
336336
const resubscribable_reactive_store_unsubscribers = reactive_stores
337337
.filter(store => {
338338
const variable = component.var_lookup.get(store.name.slice(1));
339-
return variable.reassigned;
339+
return variable && variable.reassigned;
340340
})
341341
.map(({ name }) => `$$self.$$.on_destroy.push(() => $$unsubscribe_${name.slice(1)}());`);
342342

@@ -370,7 +370,7 @@ export default function dom(
370370
const name = $name.slice(1);
371371

372372
const store = component.var_lookup.get(name);
373-
if (store.reassigned) {
373+
if (store && store.reassigned) {
374374
return `${$name}, $$unsubscribe_${name} = @noop, $$subscribe_${name} = () => { $$unsubscribe_${name}(); $$unsubscribe_${name} = ${name}.subscribe($$value => { ${$name} = $$value; $$invalidate('${$name}', ${$name}); }) }`
375375
}
376376

src/compile/render-ssr/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ export default function ssr(
2727
const reactive_stores = component.vars.filter(variable => variable.name[0] === '$' && variable.name[1] !== '$');
2828
const reactive_store_values = reactive_stores
2929
.map(({ name }) => {
30-
const store = component.var_lookup.get(name.slice(1));
31-
if (store.hoistable) return;
30+
const store_name = name.slice(1);
31+
const store = component.var_lookup.get(store_name);
32+
if (store && store.hoistable) return;
3233

33-
const assignment = `${name} = @get_store_value(${store.name});`;
34+
const assignment = `${name} = @get_store_value(${store_name});`;
3435

3536
return component.compileOptions.dev
36-
? `@validate_store(${store.name}, '${store.name}'); ${assignment}`
37+
? `@validate_store(${store_name}, '${store_name}'); ${assignment}`
3738
: assignment;
3839
});
3940

@@ -95,10 +96,11 @@ export default function ssr(
9596
const blocks = [
9697
reactive_stores.length > 0 && `let ${reactive_stores
9798
.map(({ name }) => {
98-
const store = component.var_lookup.get(name.slice(1));
99-
if (store.hoistable) {
99+
const store_name = name.slice(1);
100+
const store = component.var_lookup.get(store_name);
101+
if (store && store.hoistable) {
100102
const get_store_value = component.helper('get_store_value');
101-
return `${name} = ${get_store_value}(${store.name})`;
103+
return `${name} = ${get_store_value}(${store_name})`;
102104
}
103105
return name;
104106
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
error: 'missingGlobal is not defined'
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{$missingGlobal}</p>

0 commit comments

Comments
 (0)