Skip to content

Commit a02a745

Browse files
arturovtalxhub
authored andcommitted
fix(animations): remove finish listener once player is destroyed (#51136)
This commit removes the `finish` listener from the Animation object once the animation is finished, effectively resolving a memory leak. Previously, the `finish` listener captured `this`, which prevented `this` from being garbage collected. PR Close #51136
1 parent 664099b commit a02a745

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/animations/browser/src/render/web_animations/web_animations_player.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ export class WebAnimationsPlayer implements AnimationPlayer {
6565
// @ts-expect-error overwriting a readonly property
6666
this.domPlayer = this._triggerWebAnimation(this.element, keyframes, this.options);
6767
this._finalKeyframe = keyframes.length ? keyframes[keyframes.length - 1] : new Map();
68-
this.domPlayer.addEventListener('finish', () => this._onFinish());
68+
const onFinish = () => this._onFinish();
69+
this.domPlayer.addEventListener('finish', onFinish);
70+
this.onDestroy(() => {
71+
// We must remove the `finish` event listener once an animation has completed all its
72+
// iterations. This action is necessary to prevent a memory leak since the listener captures
73+
// `this`, creating a closure that prevents `this` from being garbage collected.
74+
this.domPlayer.removeEventListener('finish', onFinish);
75+
});
6976
}
7077

7178
private _preparePlayerBeforeStart() {

0 commit comments

Comments
 (0)