Skip to content

Commit b62368d

Browse files
committed
fix: streamline timeout handling in useOnline hook and add abort logic in getInternetConnectivity
1 parent ec20c3b commit b62368d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/components/common/hooks/useOnline/useOnline.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
1212
const timeoutRef = useRef<NodeJS.Timeout>(null)
1313
const { isAirgapped } = useMainContext()
1414

15+
const handleClearTimeout = () => {
16+
if (timeoutRef.current) {
17+
clearTimeout(timeoutRef.current)
18+
}
19+
abortControllerRef.current.abort()
20+
}
21+
1522
const checkConnectivity = async () => {
1623
if (isAirgapped) return
17-
// Cancel any pending request
18-
if (abortControllerRef.current) {
19-
abortControllerRef.current.abort()
20-
}
2124

25+
handleClearTimeout()
2226
const controller = new AbortController()
2327
abortControllerRef.current = controller
2428

@@ -39,10 +43,7 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
3943
}
4044
}
4145
const handleOffline = () => {
42-
if (timeoutRef.current) {
43-
clearTimeout(timeoutRef.current)
44-
}
45-
abortControllerRef.current.abort()
46+
handleClearTimeout()
4647
setOnline(false)
4748
}
4849

@@ -51,6 +52,11 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
5152
await checkConnectivity()
5253
}
5354

55+
useEffect(() => {
56+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
57+
handleOnline()
58+
}, [])
59+
5460
useEffect(() => {
5561
if (isAirgapped) return null
5662
window.addEventListener('online', handleOnline)

src/services/service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ export const getTemplateOptions = (appId: number, envId: number): Promise<Respon
559559
get(getUrlWithSearchParams(Routes.DEPLOYMENT_OPTIONS, { appId, envId }))
560560

561561
export const getInternetConnectivity = (controller: AbortController): Promise<any> => {
562+
setTimeout(() => {
563+
controller.abort()
564+
}, 10000)
565+
562566
return fetch(`${window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'}/${Routes.HEALTH}`, {
563567
signal: controller.signal,
564568
}).then((res) => res.json())

0 commit comments

Comments
 (0)