Skip to content
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

store direct property assignment #5416

Merged
merged 4 commits into from Sep 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Support `_` as numeric separator ([#5407](https://github.com/sveltejs/svelte/issues/5407))
* Fix assignments to properties on store values ([#5412](https://github.com/sveltejs/svelte/issues/5412))
* Support `import.meta` in template expressions ([#5422](https://github.com/sveltejs/svelte/issues/5422))

## 3.25.1
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/invalidate.ts
Expand Up @@ -62,7 +62,7 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
}

let invalidate = is_store_value
? x`@set_store_value(${head.name.slice(1)}, ${node}, ${extra_args})`
? x`@set_store_value(${head.name.slice(1)}, ${node}, ${head.name})`
: !main_execution_context
? x`$$invalidate(${renderer.context_lookup.get(head.name).index}, ${node}, ${extra_args})`
: extra_args.length
Expand Down
32 changes: 32 additions & 0 deletions test/runtime/samples/store-assignment-updates-property/_config.js
@@ -0,0 +1,32 @@
export default {
html: `
<p>a: {"foo":3,"bar":2}</p>
<p>b: {"foo":3}</p>
<button></button>
<button></button>
`,
skip_if_ssr: true,

async test({ assert, component, target, window }) {
const [btn1, btn2] = target.querySelectorAll('button');
const click = new window.MouseEvent('click');

await btn1.dispatchEvent(click);

assert.htmlEqual(target.innerHTML, `
<p>a: {"foo":4,"bar":2}</p>
<p>b: {"foo":4,"baz":0}</p>
<button></button>
<button></button>
`);

await btn2.dispatchEvent(click);

assert.htmlEqual(target.innerHTML, `
<p>a: {"foo":5,"bar":2}</p>
<p>b: {"foo":5,"qux":0}</p>
<button></button>
<button></button>
`);
}
};
24 changes: 24 additions & 0 deletions test/runtime/samples/store-assignment-updates-property/main.svelte
@@ -0,0 +1,24 @@
<script>
import { writable } from '../../../../store';

const a = writable({ foo: 1, bar: 2 });
$a.foo = 3;

const b = writable({ foo: 1, bar: 2 });
$b = { foo: 3 };

function update() {
$a.foo = $a.foo + 1;
$b = { foo: $b.foo + 1, qux: 0 };
}
</script>

<p>a: {JSON.stringify($a)}</p>
<p>b: {JSON.stringify($b)}</p>

<button on:click={() => {
$a.foo = $a.foo + 1;
$b = { foo: $b.foo + 1, baz: 0 };
}} />

<button on:click={update} />