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

fix invalidating stores with UpdateExpression #2634

Merged
merged 1 commit into from
May 4, 2019
Merged
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
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}