|
1 | | -import { useCallback, useEffect, useState } from 'react' |
| 1 | +import { useCallback, useEffect, useRef, useState } from 'react' |
2 | 2 | import { needsUpdateStore } from './needsUpdateStore' |
3 | 3 | import { NotificationRepository } from '@/notifications/domain/repositories/NotificationRepository' |
4 | 4 | import { User } from '@/users/domain/models/User' |
5 | 5 | import { useNeedsUpdate } from '@/notifications/domain/hooks/useNeedsUpdate' |
6 | 6 | import { getUnreadNotificationsCount } from '@/notifications/domain/useCases/getUnreadNotificationsCount' |
7 | 7 |
|
8 | 8 | const POLLING_INTERVAL = 30000 // 30 seconds |
| 9 | +const INVALIDATION_RETRY_INTERVAL = 1000 |
| 10 | +const INVALIDATION_RETRY_ATTEMPTS = 6 |
9 | 11 | export function useUnreadCount(user: User, notificationRepository: NotificationRepository) { |
10 | 12 | const [unreadCount, setUnreadCount] = useState(0) |
| 13 | + const retryTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null) |
| 14 | + const refreshCycleRef = useRef(0) |
11 | 15 |
|
12 | 16 | const needsUpdate = useNeedsUpdate() |
13 | | - const fetchUnread = useCallback(async () => { |
14 | | - if (user) { |
15 | | - const count = await getUnreadNotificationsCount(notificationRepository) |
16 | | - setUnreadCount(count) |
| 17 | + |
| 18 | + const clearRetryTimeout = useCallback(() => { |
| 19 | + if (retryTimeoutRef.current) { |
| 20 | + clearTimeout(retryTimeoutRef.current) |
| 21 | + retryTimeoutRef.current = null |
17 | 22 | } |
18 | | - needsUpdateStore.setNeedsUpdate(false) |
19 | | - }, [user, notificationRepository]) |
| 23 | + }, []) |
| 24 | + |
| 25 | + const fetchUnread = useCallback( |
| 26 | + async (refreshCycleId?: number) => { |
| 27 | + if (user) { |
| 28 | + const count = await getUnreadNotificationsCount(notificationRepository) |
| 29 | + if (refreshCycleId === undefined || refreshCycleRef.current === refreshCycleId) { |
| 30 | + setUnreadCount(count) |
| 31 | + } |
| 32 | + } |
| 33 | + }, |
| 34 | + [user, notificationRepository] |
| 35 | + ) |
| 36 | + |
20 | 37 | useEffect(() => { |
21 | 38 | if (needsUpdate) { |
22 | | - void fetchUnread() |
| 39 | + const refreshCycleId = refreshCycleRef.current + 1 |
| 40 | + refreshCycleRef.current = refreshCycleId |
| 41 | + clearRetryTimeout() |
| 42 | + needsUpdateStore.setNeedsUpdate(false) |
| 43 | + |
| 44 | + const runRefreshCycle = async (attempt: number) => { |
| 45 | + await fetchUnread(refreshCycleId) |
| 46 | + |
| 47 | + if (refreshCycleRef.current !== refreshCycleId) { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + if (attempt < INVALIDATION_RETRY_ATTEMPTS - 1) { |
| 52 | + retryTimeoutRef.current = setTimeout(() => { |
| 53 | + void runRefreshCycle(attempt + 1) |
| 54 | + }, INVALIDATION_RETRY_INTERVAL) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + void runRefreshCycle(0) |
23 | 59 | } |
24 | | - }, [needsUpdate, fetchUnread, notificationRepository]) |
| 60 | + }, [needsUpdate, fetchUnread, clearRetryTimeout]) |
| 61 | + |
25 | 62 | // Polling trigger |
26 | 63 | useEffect(() => { |
27 | 64 | const interval = setInterval(() => { |
28 | 65 | void fetchUnread() |
29 | 66 | }, POLLING_INTERVAL) |
30 | 67 |
|
31 | 68 | return () => clearInterval(interval) |
32 | | - }, [fetchUnread, notificationRepository]) |
| 69 | + }, [fetchUnread]) |
33 | 70 |
|
34 | 71 | useEffect(() => { |
35 | 72 | void fetchUnread() // run once when the component mounts |
36 | 73 | }, [fetchUnread]) |
37 | 74 |
|
| 75 | + useEffect(() => clearRetryTimeout, [clearRetryTimeout]) |
| 76 | + |
38 | 77 | return unreadCount |
39 | 78 | } |
0 commit comments