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

rerun reactive declarations if the reactive declared variable is mutated #5045

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,21 @@ export default class Component {
let scope = this.instance_scope;
const map = this.instance_scope_map;

const assignee_stack: Array<{
has_mutated_assignee: boolean,
}> = [];

const new_assignee_stack = () => assignee_stack.push({
has_mutated_assignee: false,
});

const update_assignee_stack = (name) => {
const variable = component.var_lookup.get(name);
if (variable) {
if (variable.mutated) assignee_stack[assignee_stack.length - 1].has_mutated_assignee = true;
}
};

walk(node.body, {
enter(node: Node, parent) {
if (map.has(node)) {
Expand All @@ -1365,9 +1380,13 @@ export default class Component {
if (node.type === 'AssignmentExpression') {
const left = get_object(node.left);

new_assignee_stack();

extract_identifiers(left).forEach(node => {
assignee_nodes.add(node);
assignees.add(node.name);

update_assignee_stack(node.name);
});

if (node.operator !== '=') {
Expand All @@ -1376,6 +1395,8 @@ export default class Component {
} else if (node.type === 'UpdateExpression') {
const identifier = get_object(node.argument);
assignees.add(identifier.name);
new_assignee_stack();
update_assignee_stack(identifier.name);
} else if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) {
const identifier = get_object(node);
if (!assignee_nodes.has(identifier)) {
Expand All @@ -1390,6 +1411,9 @@ export default class Component {
should_add_as_dependency = false;
module_dependencies.add(name);
}
if (owner === component.instance_scope && assignee_stack.length) {
if (assignee_stack[assignee_stack.length - 1].has_mutated_assignee) variable.mutated = true;
}
}
const is_writable_or_mutated =
variable && (variable.writable || variable.mutated);
Expand All @@ -1410,6 +1434,9 @@ export default class Component {
if (map.has(node)) {
scope = scope.parent;
}
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') {
assignee_stack.pop();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
export default {
html: `
<div>
<input type="checkbox">
<input type="text">
<p>one</p>
</div>
<div>
<input type="checkbox">
<input type="text">
<p>two</p>
</div>
<div>
<input type="checkbox">
<input type="text">
<p>three</p>
</div>
<p>completed 1, remaining 2, total 3</p>
`,

ssrHtml: `
<div>
<input type="checkbox">
<input type="text" value="one">
<p>one</p>
</div>
<div>
<input checked="" type="checkbox">
<input type="text" value="two">
<p>two</p>
</div>
<div>
<input type="checkbox">
<input type="text" value="three">
<p>three</p>
</div>
<p>completed 1, remaining 2, total 3</p>
`,

async test({ assert, component, target, window }) {
function set_text(i, text) {
const input = target.querySelectorAll('input[type="text"]')[i];
input.value = text;
input.dispatchEvent(new window.Event('input'));
}

function set_done(i, done) {
const input = target.querySelectorAll('input[type="checkbox"]')[i];
input.checked = done;
input.dispatchEvent(new window.Event('change'));
}

component.filter = 'remaining';

assert.htmlEqual(target.innerHTML, `
<div>
<input type="checkbox">
<input type="text">
<p>one</p>
</div>
<div>
<input type="checkbox">
<input type="text">
<p>three</p>
</div>
<p>completed 1, remaining 2, total 3</p>
`);

await set_text(1, 'four');

assert.htmlEqual(target.innerHTML, `
<div>
<input type="checkbox">
<input type="text">
<p>one</p>
</div>
<div>
<input type="checkbox">
<input type="text">
<p>four</p>
</div>
<p>completed 1, remaining 2, total 3</p>
`);

await set_done(0, true);

assert.htmlEqual(target.innerHTML, `
<div>
<input type="checkbox">
<input type="text">
<p>four</p>
</div>
<p>completed 2, remaining 1, total 3</p>
`);

component.filter = 'done';

assert.htmlEqual(target.innerHTML, `
<div>
<input type="checkbox">
<input type="text">
<p>one</p>
</div>
<div>
<input type="checkbox">
<input type="text">
<p>two</p>
</div>
<p>completed 2, remaining 1, total 3</p>
`);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
let items = [
{ done: false, text: 'one' },
{ done: true, text: 'two' },
{ done: false, text: 'three' }
];
export let filter = 'all';

$: done = items.filter(item => item.done);
$: remaining = items.filter(item => !item.done);

$: filtered = (
filter === 'all' ? items :
filter === 'done' ? items.filter(item => item.done) :
items.filter(item => !item.done)
);

</script>

{#each filtered as item}
<div>
<input type="checkbox" bind:checked={item.done}>
<input type="text" bind:value={item.text}>
<p>{item.text}</p>
</div>
{/each}

<p>completed {done.length}, remaining {remaining.length}, total {items.length}</p>