( onReconnect?: (isInitialConnection: boolean) => void, )
| 42 | * the initial connection (true) or a subsequent reconnection (false). |
| 43 | */ |
| 44 | export const useConnectionStatus = ( |
| 45 | onReconnect?: (isInitialConnection: boolean) => void, |
| 46 | ) => { |
| 47 | const [isConnected, setIsConnected] = useState(true) |
| 48 | // null = never connected, false = was disconnected, true = was connected |
| 49 | const previousConnectedRef = useRef<boolean | null>(null) |
| 50 | |
| 51 | useEffect(() => { |
| 52 | let isMounted = true |
| 53 | let timeoutId: NodeJS.Timeout | null = null |
| 54 | let consecutiveSuccesses = 0 |
| 55 | let currentInterval: number = HEALTH_CHECK_CONFIG.INITIAL_INTERVAL |
| 56 | |
| 57 | const scheduleNextCheck = (interval: number) => { |
| 58 | if (!isMounted) return |
| 59 | timeoutId = setTimeout(() => checkConnection(), interval) |
| 60 | } |
| 61 | |
| 62 | const checkConnection = async () => { |
| 63 | const client = await getCodebuffClient() |
| 64 | if (!client) { |
| 65 | if (isMounted) { |
| 66 | setIsConnected(false) |
| 67 | previousConnectedRef.current = false |
| 68 | consecutiveSuccesses = 0 |
| 69 | currentInterval = HEALTH_CHECK_CONFIG.INITIAL_INTERVAL |
| 70 | logger.debug( |
| 71 | { interval: currentInterval }, |
| 72 | 'Health check: No client, reset to initial interval', |
| 73 | ) |
| 74 | scheduleNextCheck(currentInterval) |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | const connected = await client.checkConnection() |
| 81 | if (!isMounted) return |
| 82 | |
| 83 | const prevConnected = previousConnectedRef.current |
| 84 | setIsConnected(connected) |
| 85 | previousConnectedRef.current = connected |
| 86 | |
| 87 | if (connected) { |
| 88 | // Determine if this is the initial connection (null) or a reconnection (false) |
| 89 | const isInitialConnection = prevConnected === null |
| 90 | const shouldFireReconnectCallback = |
| 91 | typeof onReconnect === 'function' && prevConnected !== true |
| 92 | |
| 93 | if (shouldFireReconnectCallback) { |
| 94 | logger.info( |
| 95 | { isInitialConnection }, |
| 96 | 'Reconnection detected, firing onReconnect callback', |
| 97 | ) |
| 98 | onReconnect(isInitialConnection) |
| 99 | } |
| 100 | consecutiveSuccesses++ |
| 101 | const newInterval = getNextInterval(consecutiveSuccesses) |
no test coverage detected