optimise certain $$invalidate calls

pull/2398/head
Richard Harris 6 years ago
parent 30c8fe0c6e
commit b36d24032b

@ -188,6 +188,12 @@ export default function dom(
code.overwrite(node.start, node.end, dirty.map(n => component.invalidate(n)).join('; ')); code.overwrite(node.start, node.end, dirty.map(n => component.invalidate(n)).join('; '));
} else { } else {
const single = (
node.left.type === 'Identifier' &&
parent.type === 'ExpressionStatement' &&
node.left.name[0] !== '$'
);
names.forEach(name => { names.forEach(name => {
const owner = scope.find_owner(name); const owner = scope.find_owner(name);
if (owner && owner !== component.instance_scope) return; if (owner && owner !== component.instance_scope) return;
@ -195,7 +201,13 @@ export default function dom(
const variable = component.var_lookup.get(name); const variable = component.var_lookup.get(name);
if (variable && (variable.hoistable || variable.global || variable.module)) return; if (variable && (variable.hoistable || variable.global || variable.module)) return;
if (single) {
code.prependRight(node.start, `$$invalidate('${name}', `);
code.appendLeft(node.end, `)`);
} else {
pending_assignments.add(name); pending_assignments.add(name);
}
component.has_reactive_assignments = true; component.has_reactive_assignments = true;
}); });
} }

@ -70,7 +70,7 @@ function instance($$self, $$props, $$invalidate) {
}; };
$$self.$$.update = ($$dirty = { foo: 1 }) => { $$self.$$.update = ($$dirty = { foo: 1 }) => {
if ($$dirty.foo) { bar = foo * 2; $$invalidate('bar', bar); } if ($$dirty.foo) { $$invalidate('bar', bar = foo * 2); }
}; };
return { foo, bar }; return { foo, bar };

@ -61,7 +61,7 @@ function instance($$self, $$props, $$invalidate) {
let x = 0; let x = 0;
function foo() { function foo() {
if (true) { x += 1; $$invalidate('x', x); } if (true) $$invalidate('x', x += 1);
} }
return { x, foo }; return { x, foo };

@ -28,8 +28,8 @@ function instance($$self, $$props, $$invalidate) {
}; };
$$self.$$.update = ($$dirty = { x: 1, b: 1 }) => { $$self.$$.update = ($$dirty = { x: 1, b: 1 }) => {
if ($$dirty.x) { b = x; $$invalidate('b', b); } if ($$dirty.x) { $$invalidate('b', b = x); }
if ($$dirty.b) { a = b; $$invalidate('a', a); } if ($$dirty.b) { $$invalidate('a', a = b); }
}; };
return { x }; return { x };

@ -27,7 +27,7 @@ function instance($$self, $$props, $$invalidate) {
let max; let max;
$$self.$$.update = ($$dirty = { a: 1, b: 1 }) => { $$self.$$.update = ($$dirty = { a: 1, b: 1 }) => {
if ($$dirty.a || $$dirty.b) { max = Math.max(a, b); $$invalidate('max', max); } if ($$dirty.a || $$dirty.b) { $$invalidate('max', max = Math.max(a, b)); }
}; };
return {}; return {};

Loading…
Cancel
Save