()
| 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) |
| 102 | |
| 103 | if (newInterval !== currentInterval) { |
| 104 | currentInterval = newInterval |
| 105 | } |
| 106 | |
| 107 | scheduleNextCheck(currentInterval) |
| 108 | } else { |
| 109 | // Reset to fast polling on connection failure |
| 110 | previousConnectedRef.current = false |
| 111 | consecutiveSuccesses = 0 |
| 112 | currentInterval = HEALTH_CHECK_CONFIG.INITIAL_INTERVAL |
| 113 | logger.debug( |
| 114 | { interval: currentInterval }, |
| 115 | 'Health check failed, reset to initial interval', |
| 116 | ) |
| 117 | scheduleNextCheck(currentInterval) |
| 118 | } |
| 119 | } catch (error) { |
no test coverage detected