cache animation param

pull/5840/head
Tan Li Hau 5 years ago
parent b928a843c8
commit e07fd2bbd2

@ -851,7 +851,21 @@ export default class ElementWrapper extends Wrapper {
${outro && b`@add_transform(${this.var}, ${rect});`} ${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); const name = this.renderer.reference(this.node.animation.name);

@ -43,7 +43,8 @@ function create_each_block(key_1, ctx) {
insert(target, div, anchor); insert(target, div, anchor);
append(div, t); 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); if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[1].name + "")) set_data(t, t_value);
}, },
r() { r() {

@ -39,7 +39,8 @@ function create_each_block(key_1, ctx) {
insert(target, div, anchor); insert(target, div, anchor);
append(div, t); 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); if (dirty & /*things*/ 1 && t_value !== (t_value = /*thing*/ ctx[1].name + "")) set_data(t, t_value);
}, },
d(detaching) { d(detaching) {

@ -56,5 +56,29 @@ export default {
raf.tick(150); raf.tick(150);
assert.equal(divs[0].dy, 0); assert.equal(divs[0].dy, 0);
assert.equal(divs[4].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);
} }
}; };

Loading…
Cancel
Save