Skip to content

Commit

Permalink
fixed refresh hooks bug
Browse files Browse the repository at this point in the history
  • Loading branch information
alimaktabi committed Feb 15, 2024
1 parent e65893a commit 6840565
Showing 1 changed file with 61 additions and 37 deletions.
98 changes: 61 additions & 37 deletions utils/hooks/refresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 6840565

Please sign in to comment.