Skip to content

Commit c67f534

Browse files
authored
Merge pull request #2755 from EmilTholin/mutated-const-reative-dependency
Set mutated const variables as reactive dependencies
2 parents bf03847 + 643aa41 commit c67f534

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/compile/Component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,11 @@ export default class Component {
11141114
if (!assignee_nodes.has(identifier)) {
11151115
const { name } = identifier;
11161116
const owner = scope.find_owner(name);
1117+
const component_var = component.var_lookup.get(name);
1118+
const is_writable_or_mutated = component_var && (component_var.writable || component_var.mutated);
11171119
if (
11181120
(!owner || owner === component.instance_scope) &&
1119-
(name[0] === '$' || component.var_lookup.has(name) && component.var_lookup.get(name).writable)
1121+
(name[0] === '$' || is_writable_or_mutated)
11201122
) {
11211123
dependencies.add(name);
11221124
}

src/compile/render-dom/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export default function dom(
364364
}
365365

366366
const variable = component.var_lookup.get(n);
367-
return variable && variable.writable;
367+
return variable && (variable.writable || variable.mutated);
368368
})
369369
.map(n => `$$dirty.${n}`).join(' || ');
370370

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
html: `
3+
<button>Mutate a</button>
4+
<div>{}</div>
5+
`,
6+
7+
async test({ assert, target }) {
8+
const button = target.querySelector('button');
9+
const click = new window.MouseEvent('click');
10+
11+
await button.dispatchEvent(click);
12+
assert.htmlEqual(target.innerHTML, `
13+
<button>Mutate a</button>
14+
<div>{"foo":42}</div>
15+
`);
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
const a = {};
3+
const b = {};
4+
$: b.foo = a.foo;
5+
</script>
6+
7+
<button on:click={() => a.foo = 42}>Mutate a</button>
8+
<div>{JSON.stringify(b)}</div>
9+

0 commit comments

Comments
 (0)