๐ ๊ธฐ๋ฅ ์์ฝ
- ํน์ ๋ถ(
00, 15, 30, 45๋ถ)๋ง๋ค ์๋์ผ๋กrefetch๋ฅผ ์คํํ๋ ํ - ์ฌ๋ฌ ๊ฐ์
refetchํจ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ์ ํ ๋ฒ์ ์คํ ๊ฐ๋ฅ - ์๋์ผ๋ก ๋ค์ ์คํ ์๊ฐ์ ๊ณ์ฐํ์ฌ ์ฃผ๊ธฐ์ ์ผ๋ก ํธ์ถ
๐ ๊ตฌํ ๋ฐฉ์
- ๋ค์ ์คํ ์๊ฐ ๊ณ์ฐ
- ํ์ฌ
๋ถ๊ณผ์ด๋ฅผ ๊ฐ์ ธ์minutes๋ฐฐ์ด์์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ค์ ๋ถ์ ์ฐพ์ - ํ์ฌ๋ณด๋ค ๋ฏธ๋์ ๋ถ์ด ์์ผ๋ฉด, ๋ค์ ์๊ฐ(
hour)์ ์ฒซ ๋ฒ์งธ ๋ถ์ผ๋ก ์ค์ - ๋จ์ ์๊ฐ(ms) ๊ณ์ฐ ํ
setTimeout์ผ๋ก ์์ฝ
- ํ์ฌ
- ์๋ ์คํ
- ์์ฝ๋ ์๊ฐ์ด ๋๋ฉด ๋ชจ๋
refetchํจ์ ์คํ - ์คํ ํ ๋ค์
scheduleNextRefetch()ํธ์ถํ์ฌ ๋ค์ ์์ฝ์ ์ค์
- ์์ฝ๋ ์๊ฐ์ด ๋๋ฉด ๋ชจ๋
- ํจ์จ์ ์ธ ํ์ด๋จธ ๊ด๋ฆฌ
- ๊ธฐ์กด ํ์ด๋จธ๊ฐ ์์ผ๋ฉด
clearTimeout์ผ๋ก ์ ๊ฑฐ ํ ์๋ก ์ค์ useEffect์์minutes๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ํ์ด๋จธ๋ฅผ ์ด๊ธฐํํ์ฌ ์ฌ๊ณ์ฐ
- ๊ธฐ์กด ํ์ด๋จธ๊ฐ ์์ผ๋ฉด
๐ก ํ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64import { useCallback, useEffect, useRef } from "react"; interface UseScheduledRefetchProps { refetch: (() => void)[]; // refetch ํจ์๋ค์ ๋ฐฐ์ด minutes: number[]; } const useScheduledRefetch = ({ refetch, minutes, }: UseScheduledRefetchProps) => { const timeoutRef = useRef<NodeJS.Timeout | null>(null); // ๋ค์ ๋ฆฌํจ์น ์๊ฐ์ ๊ณ์ฐํ๋ ํจ์ const scheduleNextRefetch = useCallback(() => { if (minutes.length === 0) return; // minutes๊ฐ ๋น์ด์์ผ๋ฉด ๋ฆฌํด const now = new Date(); const currentMinute = now.getMinutes(); const currentSecond = now.getSeconds(); // ํ์ฌ๋ณด๋ค ๋ฏธ๋์ธ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ ์ฐพ๊ธฐ let nextMinute = minutes.find((m) => m > currentMinute); // ํ์ฌ ์๊ฐ ์ดํ์ ์คํํ ๋ถ์ด ์์ผ๋ฉด ๋ค์ ์๊ฐ์ ์ฒซ ๋ฒ์งธ ๋ถ์ผ๋ก ์ค์ if (!nextMinute) { nextMinute = minutes[0]; } // ๋ค์ ์คํ๊น์ง ๋จ์ ์๊ฐ(ms) ๊ณ์ฐ let msUntilNext; if (nextMinute > currentMinute) { // ๊ฐ์ ์๊ฐ ๋ด ๋ค์ ๋ถ msUntilNext = ((nextMinute - currentMinute) * 60 - currentSecond) * 1000; } else { // ๋ค์ ์๊ฐ์ ์ฒซ ๋ถ (์: ํ์ฌ 45๋ถ์ด๊ณ ๋ค์์ 00๋ถ) msUntilNext = ((60 - currentMinute + nextMinute) * 60 - currentSecond) * 1000; } // ์ด์ ํ์ด๋จธ ์ ๋ฆฌ if (timeoutRef.current) clearTimeout(timeoutRef.current); timeoutRef.current = setTimeout(() => { // ๋ชจ๋ refetch ํจ์ ์คํ refetch.forEach((fn) => fn()); scheduleNextRefetch(); // ๋ค์ ๋ฆฌํจ์น ์์ฝ }, msUntilNext); }, [minutes, refetch]); useEffect(() => { // minutes๊ฐ ๋ฐ๋๋ฉด ์ฆ์ ๋ค์ ์ค์ผ์ค ์ฌ๊ณ์ฐ scheduleNextRefetch(); // ์ ๋ฆฌ ํจ์ return () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); }; }, [scheduleNextRefetch, minutes]); return null; }; export default useScheduledRefetch
๐ก ํ์ฉ ์์
1 2 3 4 5 6 7const { data, refetch } = useQuery("fetchData", fetchDataFn); const { data: chartData, refetch: chartRefetch } = useQuery("fetchChart", fetchChartFn); useScheduledRefetch({ refetch: [refetch, chartRefetch], minutes: [0, 15, 30, 45], });
โ ํน์ง ๋ฐ ์ฅ์
- ์ฌ์ฌ์ฉ์ฑ ์ฆ๊ฐ:
refetchํจ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ์ ์ฌ๋ฌ ์์ฒญ์ ํ ๋ฒ์ ์ฒ๋ฆฌ ๊ฐ๋ฅ - ์ฝ๋ ์ค๋ณต ๊ฐ์: ์ฌ๋ฌ ๊ณณ์์ ํ ์ ์ฌ์ฉํ ํ์ ์์ด ํ ๋ฒ๋ง ์ ์ธ
- ์๋ ์ค์ผ์ค๋ง: ๋งค ์๊ฐ
00, 15, 30, 45๋ถ์ ๋ง์ถฐ ํธ์ถ๋จ - ํ์ด๋จธ ๊ด๋ฆฌ ์ต์ ํ: ๊ธฐ์กด ํ์ด๋จธ๋ฅผ ์ ๋ฆฌํ๊ณ ์๋ก์ด ํ์ด๋จธ๋ฅผ ์ค์ ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐฉ์ง
โ ๏ธ์ฃผ์
- ํด๋น ๋ฆฌ์กํธ ์ฟผ๋ฆฌ ์์ฑ์ค refetchInterval ์ ํด์ ์๋ง
