From e07fd2bbd263a04448709b8b064001daef2f9f39 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Thu, 31 Dec 2020 12:34:58 +0800 Subject: [PATCH] cache animation param --- .../render_dom/wrappers/Element/index.ts | 16 ++++++++++++- .../each-block-keyed-animated/expected.js | 3 ++- test/js/samples/each-block-keyed/expected.js | 3 ++- .../samples/animation-js-delay/_config.js | 24 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/Element/index.ts b/src/compiler/compile/render_dom/wrappers/Element/index.ts index d90cf0da98..269edcfca8 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/index.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/index.ts @@ -851,7 +851,21 @@ export default class ElementWrapper extends Wrapper { ${outro && b`@add_transform(${this.var}, ${rect});`} `); - const params = this.node.animation.expression ? this.node.animation.expression.manipulate(block) : x`{}`; + let params; + if (this.node.animation.expression) { + params = this.node.animation.expression.manipulate(block); + + if (this.node.animation.expression.dynamic_dependencies().length) { + // if `params` is dynamic, calculate params ahead of time in the `.r()` method + const params_var = block.get_unique_name('params'); + block.add_variable(params_var); + + block.chunks.measure.push(b`${params_var} = ${params};`); + params = params_var; + } + } else { + params = x`{}`; + } const name = this.renderer.reference(this.node.animation.name); diff --git a/test/js/samples/each-block-keyed-animated/expected.js b/test/js/samples/each-block-keyed-animated/expected.js index 46ef63ee7f..96e75c61d3 100644 --- a/test/js/samples/each-block-keyed-animated/expected.js +++ b/test/js/samples/each-block-keyed-animated/expected.js @@ -43,7 +43,8 @@ function create_each_block(key_1, ctx) { insert(target, div, anchor); append(div, t); }, - p(ctx, dirty) { + p(new_ctx, dirty) { + ctx = new_ctx; if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[1].name + "")) set_data(t, t_value); }, r() { diff --git a/test/js/samples/each-block-keyed/expected.js b/test/js/samples/each-block-keyed/expected.js index 71853cf295..a01bec628b 100644 --- a/test/js/samples/each-block-keyed/expected.js +++ b/test/js/samples/each-block-keyed/expected.js @@ -39,7 +39,8 @@ function create_each_block(key_1, ctx) { insert(target, div, anchor); append(div, t); }, - p(ctx, dirty) { + p(new_ctx, dirty) { + ctx = new_ctx; if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[1].name + "")) set_data(t, t_value); }, d(detaching) { diff --git a/test/runtime/samples/animation-js-delay/_config.js b/test/runtime/samples/animation-js-delay/_config.js index 931d37817b..67ff52decc 100644 --- a/test/runtime/samples/animation-js-delay/_config.js +++ b/test/runtime/samples/animation-js-delay/_config.js @@ -56,5 +56,29 @@ export default { raf.tick(150); assert.equal(divs[0].dy, 0); assert.equal(divs[4].dy, 0); + + component.things = [ + { id: 1, name: 'a' }, + { id: 2, name: 'b' }, + { id: 3, name: 'c' }, + { id: 4, name: 'd' }, + { id: 5, name: 'e' } + ]; + + divs = document.querySelectorAll('div'); + assert.equal(divs[0].dy, 120); + assert.equal(divs[4].dy, -120); + + raf.tick(200); + assert.equal(divs[0].dy, 108); + assert.equal(divs[4].dy, -60); + + raf.tick(250); + assert.equal(divs[0].dy, 48); + assert.equal(divs[4].dy, 0); + + raf.tick(300); + assert.equal(divs[0].dy, 0); + assert.equal(divs[4].dy, 0); } };