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

perf(animation): optimize animation object memory #6025

Merged
merged 1 commit into from
Jul 12, 2024
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
33 changes: 30 additions & 3 deletions packages/g6/src/elements/shapes/base-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
}

const result = super.animate(keyframes, options);
if (result) animationMap.push(result);
if (result) {
releaseAnimation(this, result);
animationMap.push(result);
}

if (Array.isArray(keyframes) && keyframes.length > 0) {
// 如果 keyframes 中仅存在 skippedAttrs 中的属性,则仅更新父元素属性(跳过子图形)
Expand All @@ -170,7 +173,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
method.call(this, { ...this.attributes, ...style }),
);
const result = shape.animate(preprocessKeyframes(subKeyframes), options);
if (result) animationMap.push(result);
if (result) {
releaseAnimation(shape, result);
animationMap.push(result);
}
}
});

Expand All @@ -185,7 +191,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
const shape = shapeSet[key];
if (shape) {
const result = shape.animate(preprocessKeyframes(subKeyframes), options);
if (result) animationMap.push(result);
if (result) {
releaseAnimation(shape, result);
animationMap.push(result);
}
}
});
}
Expand Down Expand Up @@ -217,3 +226,21 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
super.destroy();
}
}

/**
* <zh/> 释放动画
*
* <en/> Release animation
* @param target - <zh/> 目标对象 | <en/> target object
* @param animation - <zh/> 动画实例 | <en/> animation instance
* @description see: https://github.com/antvis/G/issues/1731
*/
function releaseAnimation(target: DisplayObject, animation: IAnimation) {
animation.oncancel = () => console.log('cancel');
animation?.finished.then(() => {
// @ts-expect-error private property
const index = target.activeAnimations.findIndex((_) => _ === animation);
// @ts-expect-error private property
if (index > -1) target.activeAnimations.splice(index, 1);
});
}
21 changes: 21 additions & 0 deletions packages/g6/src/runtime/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class Animation {
animation.finished.then(() => {
cb?.afterAnimate?.(animation);
cb?.after?.();
this.animations.delete(animation);
});
} else cb?.after?.();

Expand All @@ -63,6 +64,7 @@ export class Animation {
animation.finished.then(() => {
callbacks?.afterAnimate?.(animation);
callbacks?.after?.();
this.release();
});
} else callbacks?.after?.();

Expand Down Expand Up @@ -147,6 +149,25 @@ export class Animation {
this.tasks = [];
}

/**
* <zh/> 释放存量动画对象
*
* <en/> Release stock animation objects
* @description see: https://github.com/antvis/G/issues/1731
*/
private release() {
const { canvas } = this.context;

// @ts-expect-error private property
const animationsWithPromises = canvas.document?.timeline?.animationsWithPromises;
if (animationsWithPromises) {
// @ts-expect-error private property
canvas.document.timeline.animationsWithPromises = animationsWithPromises.filter(
(animation: IAnimation) => animation.playState !== 'finished',
);
}
}

public destroy() {
this.stop();
this.animations.clear();
Expand Down
Loading