From d0d1a57cf3a5f6bbb757e2b7a23b5841bf74a47f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 6 Sep 2024 15:42:30 +0200 Subject: [PATCH] [Perf] Delay video cleanup with `onFullyIdle` --- src/hooks/useVideoCleanup.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/hooks/useVideoCleanup.ts b/src/hooks/useVideoCleanup.ts index 5750acf15..14d608648 100644 --- a/src/hooks/useVideoCleanup.ts +++ b/src/hooks/useVideoCleanup.ts @@ -1,21 +1,20 @@ import type { RefObject } from 'react'; import { useEffect } from '../lib/teact/teact'; -import { requestNextMutation } from '../lib/fasterdom/fasterdom'; import unloadVideo from '../util/browser/unloadVideo'; +import { onFullyIdle } from './useHeavyAnimationCheck'; // Fix for memory leak when unmounting video element export default function useVideoCleanup(videoRef: RefObject, dependencies: any[]) { useEffect(() => { const videoEl = videoRef.current; + if (!videoEl) return undefined; return () => { - if (videoEl) { - // It may be slow (specifically on iOS), so we postpone it after unmounting - requestNextMutation(() => { - unloadVideo(videoEl); - }); - } + // It may be slow (specifically on iOS), so we postpone it after unmounting + onFullyIdle(() => { + unloadVideo(videoEl); + }); }; // eslint-disable-next-line react-hooks-static-deps/exhaustive-deps }, dependencies);