Skip to content

[fix] store reactivity in reactive declarations #6559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,17 @@ export default class Component {
this.node_for_declaration.set(name, node);
});

globals.forEach((node, name) => {
// NOTE: add store variable first, then only $store value
// as `$store` will mark `store` variable as referenced and subscribable
const global_keys = Array.from(globals.keys());
const sorted_globals = [
...global_keys.filter(key => key[0] !== '$'),
...global_keys.filter(key => key[0] === '$')
];

sorted_globals.forEach(name => {
if (this.var_lookup.has(name)) return;
const node = globals.get(name);

if (this.injected_reactive_declaration_vars.has(name)) {
this.add_var({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
export let store_container;

$: ({ store } = store_container);
$: value = $store;
</script>
<div>{value}</div>
<div>{$store}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
html: `
<div>Hello World</div>
<div>Hello World</div>
`,

async test({ assert, component, target, window }) {
await component.update_value('Hi Svelte');

assert.htmlEqual(target.innerHTML, `
<div>Hi Svelte</div>
<div>Hi Svelte</div>
`);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { writable } from 'svelte/store';
import Child from './App.svelte';

const store_container = { store: writable('Hello World') };

export function update_value(value) {
store_container.store = writable(value);
}
</script>

<Child {store_container} />