Skip to content

Commit d1536b2

Browse files
authored
Merge pull request sveltejs#2307 from sveltejs/sveltejsgh-2305
invalidate correctly inside event handlers
2 parents fa47f76 + d614cfa commit d1536b2

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/compile/nodes/shared/Expression.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,18 @@ export default class Expression {
364364
let body = code.slice(node.body.start, node.body.end).trim();
365365
if (node.body.type !== 'BlockStatement') {
366366
if (pending_assignments.size > 0) {
367-
const insert = Array.from(pending_assignments).map(name => component.invalidate(name)).join('; ');
367+
const dependencies = new Set();
368+
pending_assignments.forEach(name => {
369+
if (template_scope.names.has(name)) {
370+
template_scope.dependencies_for_name.get(name).forEach(dependency => {
371+
dependencies.add(dependency);
372+
});
373+
} else {
374+
dependencies.add(name);
375+
}
376+
});
377+
378+
const insert = Array.from(dependencies).map(name => component.invalidate(name)).join('; ');
368379
pending_assignments = new Set();
369380

370381
component.has_reactive_assignments = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default {
2+
html: `
3+
<button>off</button>
4+
<button>on</button>
5+
<button>off</button>
6+
<p>on: 1</p>
7+
`,
8+
9+
async test({ assert, component, target, window }) {
10+
const buttons = target.querySelectorAll('button');
11+
const event = new window.MouseEvent('click');
12+
13+
await buttons[0].dispatchEvent(event);
14+
assert.htmlEqual(target.innerHTML, `
15+
<button>on</button>
16+
<button>on</button>
17+
<button>off</button>
18+
<p>on: 2</p>
19+
`);
20+
21+
await buttons[2].dispatchEvent(event);
22+
assert.htmlEqual(target.innerHTML, `
23+
<button>on</button>
24+
<button>on</button>
25+
<button>on</button>
26+
<p>on: 3</p>
27+
`);
28+
29+
assert.deepEqual(component.switches, [
30+
{ on: true },
31+
{ on: true },
32+
{ on: true }
33+
]);
34+
}
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
export let switches = [
3+
{ on: false },
4+
{ on: true },
5+
{ on: false }
6+
];
7+
</script>
8+
9+
{#each switches as s}
10+
<button on:click="{() => s.on = !s.on}">
11+
{s.on ? 'on' : 'off'}
12+
</button>
13+
{/each}
14+
15+
<p>on: {switches.filter(s => !!s.on).length}</p>

0 commit comments

Comments
 (0)