Skip to content

Commit 365b5e3

Browse files
authored
[fix] store reactivity in reactive declarations (#6559)
1 parent d487254 commit 365b5e3

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/compiler/compile/Component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,17 @@ export default class Component {
666666
this.node_for_declaration.set(name, node);
667667
});
668668

669-
globals.forEach((node, name) => {
669+
// NOTE: add store variable first, then only $store value
670+
// as `$store` will mark `store` variable as referenced and subscribable
671+
const global_keys = Array.from(globals.keys());
672+
const sorted_globals = [
673+
...global_keys.filter(key => key[0] !== '$'),
674+
...global_keys.filter(key => key[0] === '$')
675+
];
676+
677+
sorted_globals.forEach(name => {
670678
if (this.var_lookup.has(name)) return;
679+
const node = globals.get(name);
671680

672681
if (this.injected_reactive_declaration_vars.has(name)) {
673682
this.add_var({
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
export let store_container;
3+
4+
$: ({ store } = store_container);
5+
$: value = $store;
6+
</script>
7+
<div>{value}</div>
8+
<div>{$store}</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
html: `
3+
<div>Hello World</div>
4+
<div>Hello World</div>
5+
`,
6+
7+
async test({ assert, component, target, window }) {
8+
await component.update_value('Hi Svelte');
9+
10+
assert.htmlEqual(target.innerHTML, `
11+
<div>Hi Svelte</div>
12+
<div>Hi Svelte</div>
13+
`);
14+
}
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
import Child from './App.svelte';
4+
5+
const store_container = { store: writable('Hello World') };
6+
7+
export function update_value(value) {
8+
store_container.store = writable(value);
9+
}
10+
</script>
11+
12+
<Child {store_container} />

0 commit comments

Comments
 (0)