From c5f2b5f4dc99341a3e77ff32258e6f379efaf2fc Mon Sep 17 00:00:00 2001 From: chriskari Date: Tue, 29 Oct 2024 01:05:12 +0100 Subject: [PATCH] fix: added proper clearing of leftover intervals --- src/shared/hooks/BackendAPI/useGet.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/shared/hooks/BackendAPI/useGet.js b/src/shared/hooks/BackendAPI/useGet.js index 8c59ab065b..24ac8eb4b5 100644 --- a/src/shared/hooks/BackendAPI/useGet.js +++ b/src/shared/hooks/BackendAPI/useGet.js @@ -28,6 +28,7 @@ const useGetHook = processDataFn => const currentRequestId = uuid(); const requestData = useRef({}); const previousRequestNotFinished = useRef(null); + const intervalIdRef = useRef(null); const refetch = useCallback( (isSilent, currentData) => async () => { @@ -96,14 +97,22 @@ const useGetHook = processDataFn => [fetch, authData], ); + const cleanupPolling = useCallback(() => { + if (intervalIdRef.current) { + clearInterval(intervalIdRef.current); + intervalIdRef.current = null; + } + }, []); + useEffect(() => { const receivedForbidden = error?.code === 403; + cleanupPolling(); // POLLING if (!pollingInterval || receivedForbidden || skip) return; - const intervalId = setInterval(refetch(true, data), pollingInterval); - return _ => clearInterval(intervalId); - }, [path, pollingInterval, data, error, skip, refetch]); + intervalIdRef.current = setInterval(refetch(true, data), pollingInterval); + return cleanupPolling; + }, [path, pollingInterval, data, error, skip, refetch, cleanupPolling]); useEffect(() => { // INITIAL FETCH on path being set/changed