From 68405653a2589122484c7575f2d90da7e1b9bc79 Mon Sep 17 00:00:00 2001 From: Ali Maktabi <67747298+alimaktabi@users.noreply.github.com> Date: Thu, 15 Feb 2024 12:34:02 +0330 Subject: [PATCH] fixed refresh hooks bug --- utils/hooks/refresh.tsx | 98 +++++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/utils/hooks/refresh.tsx b/utils/hooks/refresh.tsx index 941473fe..ea72ddcd 100644 --- a/utils/hooks/refresh.tsx +++ b/utils/hooks/refresh.tsx @@ -4,69 +4,93 @@ import { MEDIUM_INTERVAL, SLOW_INTERVAL, } from "@/constants"; -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; export const useFastRefresh = ( - callback: CallableFunction, - dependencies: any[] = [] + callback: () => void, + dependencies: any[] = [], + fastInterval: number = FAST_INTERVAL ) => { + const callbackRef = useRef<() => void>(); + useEffect(() => { - const timeout = setInterval(() => { - callback(); - }, FAST_INTERVAL); + callbackRef.current = callback; + }, [callback]); - return () => { - clearInterval(timeout); - }; - }, [...dependencies, callback]); + useEffect(() => { + const intervalId = setInterval(() => { + if (callbackRef.current) { + callbackRef.current(); + } + }, fastInterval); + + return () => clearInterval(intervalId); + }, [fastInterval, ...dependencies]); }; export const useMediumRefresh = ( - callback: CallableFunction, - dependencies: any[] = [] + callback: () => void, + dependencies: any[] = [], + mediumInterval: number = MEDIUM_INTERVAL ) => { + const callbackRef = useRef<() => void>(); + useEffect(() => { - const timeout = setInterval(() => { - callback(); - }, MEDIUM_INTERVAL); + callbackRef.current = callback; + }, [callback]); - return () => { - clearInterval(timeout); - }; - }, dependencies); + useEffect(() => { + const intervalId = setInterval(() => { + if (callbackRef.current) { + callbackRef.current(); + } + }, mediumInterval); + + return () => clearInterval(intervalId); + }, [mediumInterval, ...dependencies]); }; export const useSlowRefresh = ( - callback: CallableFunction, - dependencies: any[] = [] + callback: () => void, + dependencies: any[] = [], + slowInterval: number = SLOW_INTERVAL ) => { + const callbackRef = useRef<() => void>(); + useEffect(() => { - const timeout = setInterval(() => { - callback(); - }, SLOW_INTERVAL); + callbackRef.current = callback; + }, [callback]); - return () => { - clearInterval(timeout); - }; - }, dependencies); + useEffect(() => { + const intervalId = setInterval(() => { + if (callbackRef.current) { + callbackRef.current(); + } + }, slowInterval); + + return () => clearInterval(intervalId); + }, [slowInterval, ...dependencies]); }; export const useRefreshWithInitial = ( - callback: CallableFunction, + callback: () => void, interval: IntervalType, dependencies: any[] = [] ) => { + const callbackRef = useRef<() => void>(); + useEffect(() => { - const timeout = setInterval(() => { - callback(); - }, interval); + callbackRef.current = callback; + }, [callback]); - callback(); + useEffect(() => { + if (interval && callbackRef.current) { + const intervalId = setInterval(callbackRef.current, interval); + callbackRef.current(); // Initial callback execution - return () => { - clearInterval(timeout); - }; - }, dependencies); + return () => clearInterval(intervalId); + } + }, [interval, ...dependencies]); }; export const usePreventNavigation = (isActive: boolean, message?: string) => {