MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / checkConnection

Function checkConnection

cli/src/hooks/use-connection-status.ts:62–129  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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) {

Callers 2

scheduleNextCheckFunction · 0.85
useConnectionStatusFunction · 0.85

Calls 4

getCodebuffClientFunction · 0.90
scheduleNextCheckFunction · 0.85
getNextIntervalFunction · 0.85
checkConnectionMethod · 0.80

Tested by

no test coverage detected