Skip to content

Commit

Permalink
Merge pull request #2634 from sveltejs/gh-2625
Browse files Browse the repository at this point in the history
fix invalidating stores with UpdateExpression
  • Loading branch information
Rich-Harris authored May 4, 2019
2 parents 73bf582 + 10f6da3 commit 203d0ec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/compile/render-dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ export default function dom(
scope = scope.parent;
}

if (node.type === 'AssignmentExpression') {
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') {
const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument;
let names = [];

if (node.left.type === 'MemberExpression') {
const left_object_name = get_object(node.left).name;
if (assignee.type === 'MemberExpression') {
const left_object_name = get_object(assignee).name;
left_object_name && (names = [left_object_name]);
} else {
names = extract_names(node.left);
names = extract_names(assignee);
}

if (node.operator === '=' && nodes_match(node.left, node.right)) {
Expand All @@ -189,9 +190,10 @@ export default function dom(
code.overwrite(node.start, node.end, dirty.map(n => component.invalidate(n)).join('; '));
} else {
const single = (
node.left.type === 'Identifier' &&
node.type === 'AssignmentExpression' &&
assignee.type === 'Identifier' &&
parent.type === 'ExpressionStatement' &&
node.left.name[0] !== '$'
assignee.name[0] !== '$'
);

names.forEach(name => {
Expand All @@ -213,18 +215,6 @@ export default function dom(
}
}

else if (node.type === 'UpdateExpression') {
const { name } = get_object(node.argument);

if (scope.find_owner(name) !== component.instance_scope) return;

const variable = component.var_lookup.get(name);
if (variable && variable.hoistable) return;

pending_assignments.add(name);
component.has_reactive_assignments = true;
}

if (pending_assignments.size > 0) {
if (node.type === 'ArrowFunctionExpression') {
const insert = Array.from(pending_assignments).map(name => component.invalidate(name)).join('; ');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
html: `0`,

async test({ assert, component, target }) {
await component.increment();
assert.htmlEqual(target.innerHTML, `1`);
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/store-increment-updates-reactive/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { writable } from '../../../../store.js';

const foo = writable(0);

export function increment() {
$foo++;
}
</script>

{$foo}

0 comments on commit 203d0ec

Please sign in to comment.