From b36d24032b1d9787673e6a796af23b6b3df88196 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Thu, 11 Apr 2019 23:46:38 -0400 Subject: [PATCH] optimise certain $$invalidate calls --- src/compile/render-dom/index.ts | 14 +++++++++++++- .../dev-warning-missing-data-computed/expected.js | 2 +- .../instrumentation-script-if-no-block/expected.js | 2 +- .../expected.js | 4 ++-- .../expected.js | 2 +- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/compile/render-dom/index.ts b/src/compile/render-dom/index.ts index 6aa36d88f7..d7649897c0 100644 --- a/src/compile/render-dom/index.ts +++ b/src/compile/render-dom/index.ts @@ -188,6 +188,12 @@ export default function dom( code.overwrite(node.start, node.end, dirty.map(n => component.invalidate(n)).join('; ')); } else { + const single = ( + node.left.type === 'Identifier' && + parent.type === 'ExpressionStatement' && + node.left.name[0] !== '$' + ); + names.forEach(name => { const owner = scope.find_owner(name); if (owner && owner !== component.instance_scope) return; @@ -195,7 +201,13 @@ export default function dom( const variable = component.var_lookup.get(name); if (variable && (variable.hoistable || variable.global || variable.module)) return; - pending_assignments.add(name); + if (single) { + code.prependRight(node.start, `$$invalidate('${name}', `); + code.appendLeft(node.end, `)`); + } else { + pending_assignments.add(name); + } + component.has_reactive_assignments = true; }); } diff --git a/test/js/samples/dev-warning-missing-data-computed/expected.js b/test/js/samples/dev-warning-missing-data-computed/expected.js index 1585e1ea27..12e8a983d0 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected.js @@ -70,7 +70,7 @@ function instance($$self, $$props, $$invalidate) { }; $$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 }; diff --git a/test/js/samples/instrumentation-script-if-no-block/expected.js b/test/js/samples/instrumentation-script-if-no-block/expected.js index 6ab3e37f1f..ce5948de73 100644 --- a/test/js/samples/instrumentation-script-if-no-block/expected.js +++ b/test/js/samples/instrumentation-script-if-no-block/expected.js @@ -61,7 +61,7 @@ function instance($$self, $$props, $$invalidate) { let x = 0; function foo() { - if (true) { x += 1; $$invalidate('x', x); } + if (true) $$invalidate('x', x += 1); } return { x, foo }; diff --git a/test/js/samples/reactive-values-non-topologically-ordered/expected.js b/test/js/samples/reactive-values-non-topologically-ordered/expected.js index b0bd493d0f..2f729362b0 100644 --- a/test/js/samples/reactive-values-non-topologically-ordered/expected.js +++ b/test/js/samples/reactive-values-non-topologically-ordered/expected.js @@ -28,8 +28,8 @@ function instance($$self, $$props, $$invalidate) { }; $$self.$$.update = ($$dirty = { x: 1, b: 1 }) => { - if ($$dirty.x) { b = x; $$invalidate('b', b); } - if ($$dirty.b) { a = b; $$invalidate('a', a); } + if ($$dirty.x) { $$invalidate('b', b = x); } + if ($$dirty.b) { $$invalidate('a', a = b); } }; return { x }; diff --git a/test/js/samples/reactive-values-non-writable-dependencies/expected.js b/test/js/samples/reactive-values-non-writable-dependencies/expected.js index fc6f059d4e..62057cc6f0 100644 --- a/test/js/samples/reactive-values-non-writable-dependencies/expected.js +++ b/test/js/samples/reactive-values-non-writable-dependencies/expected.js @@ -27,7 +27,7 @@ function instance($$self, $$props, $$invalidate) { let max; $$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 {};