From 37617b4b8e02f369e31fab5d170809f051b9ab0d Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Thu, 13 Feb 2025 14:28:05 +0100 Subject: [PATCH] Fix several potential memory leaks (#5586) --- src/components/story/helpers/ribbonAnimation.ts | 4 ++++ src/hooks/useIntersectionObserver.ts | 1 + src/lib/teact/teact.ts | 2 ++ src/util/animateHorizontalScroll.ts | 15 +++++++++++---- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/story/helpers/ribbonAnimation.ts b/src/components/story/helpers/ribbonAnimation.ts index 057bf73e4..738e59177 100644 --- a/src/components/story/helpers/ribbonAnimation.ts +++ b/src/components/story/helpers/ribbonAnimation.ts @@ -144,6 +144,8 @@ export function animateOpening(isArchived?: boolean) { } toggleAvatar?.classList.remove('animating'); peer.classList.remove('animating'); + + callbacks.delete(cb); }); }, ANIMATION_DURATION + ANIMATION_END_DELAY); @@ -283,6 +285,8 @@ export function animateClosing(isArchived?: boolean) { peer.classList.remove('animating'); toggleAvatar!.classList.remove('animating'); }); + + callbacks.delete(cb); }, ANIMATION_DURATION + ANIMATION_END_DELAY); callbacks.add(cb); diff --git a/src/hooks/useIntersectionObserver.ts b/src/hooks/useIntersectionObserver.ts index 88f960585..027a2506f 100644 --- a/src/hooks/useIntersectionObserver.ts +++ b/src/hooks/useIntersectionObserver.ts @@ -163,6 +163,7 @@ export function useIntersectionObserver({ function destroy() { callbacks.clear(); observer.disconnect(); + entriesAccumulator.clear(); } controllerRef.current = { diff --git a/src/lib/teact/teact.ts b/src/lib/teact/teact.ts index da37c47a6..64c96eba5 100644 --- a/src/lib/teact/teact.ts +++ b/src/lib/teact/teact.ts @@ -722,6 +722,8 @@ function useEffectBase( }; } + if (effectConfig) effectConfig.schedule = undefined; // Help GC + byCursor[cursor] = { ...effectConfig, dependencies, diff --git a/src/util/animateHorizontalScroll.ts b/src/util/animateHorizontalScroll.ts index 1c1c7ca4d..d000a99d2 100644 --- a/src/util/animateHorizontalScroll.ts +++ b/src/util/animateHorizontalScroll.ts @@ -57,8 +57,17 @@ export default function animateHorizontalScroll(container: HTMLElement, left: nu const startAt = Date.now(); + function cleanup() { + container.style.scrollSnapType = ''; + delete container.dataset.scrollId; + stopById.delete(id); + } + animate(() => { - if (isStopped) return false; + if (isStopped) { + cleanup(); + return false; + } const t = Math.min((Date.now() - startAt) / duration, 1); @@ -66,9 +75,7 @@ export default function animateHorizontalScroll(container: HTMLElement, left: nu container.scrollLeft = Math.round(target - currentPath); if (t >= 1) { - container.style.scrollSnapType = ''; - delete container.dataset.scrollId; - stopById.delete(id); + cleanup(); resolve(); }