Skip to content

Commit

Permalink
fix store direct property assignment (#5416)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored Sep 18, 2020
1 parent aef5671 commit b3f54bd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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} />

0 comments on commit b3f54bd

Please sign in to comment.