Skip to content

Commit 631802d

Browse files
committed
chore: service code modification
1 parent d859b89 commit 631802d

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed
Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
import { Routes } from '@Config/constants'
22

33
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
4+
import { CheckConnectivityParamsType, FetchConnectivityParamsType } from './types'
45

5-
const fetchWithTimeout = (url: string, options: RequestInit, controller: AbortController): Promise<any> => {
6+
const fetchWithTimeout = ({
7+
url,
8+
options,
9+
controller,
10+
setTimeoutRef,
11+
checkConnectivity,
12+
}: FetchConnectivityParamsType): Promise<any> => {
613
const timeoutId = setTimeout(() => controller.abort(), INTERNET_CONNECTIVITY_INTERVAL)
714

8-
return fetch(url, { ...options, signal: controller.signal }).finally(() => clearTimeout(timeoutId))
15+
return fetch(url, { ...options, signal: controller.signal }).finally(() => {
16+
clearTimeout(timeoutId)
17+
const nextRef = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
18+
setTimeoutRef(nextRef)
19+
})
920
}
1021

11-
export const getFallbackInternetConnectivity = (controller: AbortController): Promise<any> =>
12-
fetchWithTimeout(
13-
'https://www.google.com/favicon.ico',
14-
{
22+
export const getFallbackInternetConnectivity = ({
23+
controller,
24+
setTimeoutRef,
25+
checkConnectivity,
26+
}: CheckConnectivityParamsType): Promise<any> =>
27+
fetchWithTimeout({
28+
url: 'https://www.google.com/favicon.ico',
29+
options: {
1530
method: 'HEAD',
1631
mode: 'no-cors',
1732
},
1833
controller,
19-
)
34+
setTimeoutRef,
35+
checkConnectivity,
36+
})
2037

21-
export const getInternetConnectivity = (controller: AbortController): Promise<any> => {
38+
export const getInternetConnectivity = ({
39+
controller,
40+
setTimeoutRef,
41+
checkConnectivity,
42+
}: CheckConnectivityParamsType): Promise<any> => {
2243
const baseUrl = window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'
2344
const url = `${baseUrl}/${Routes.HEALTH}`
2445

25-
return fetchWithTimeout(url, {}, controller).then((res) => res.json())
46+
return fetchWithTimeout({ url, options: {}, controller, setTimeoutRef, checkConnectivity }).then((res) =>
47+
res.json(),
48+
)
2649
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface FetchConnectivityParamsType {
2+
url: string
3+
options: RequestInit
4+
controller: AbortController
5+
setTimeoutRef: (ref: NodeJS.Timeout) => void
6+
checkConnectivity: () => void
7+
}
8+
9+
export interface CheckConnectivityParamsType
10+
extends Pick<FetchConnectivityParamsType, 'controller' | 'setTimeoutRef' | 'checkConnectivity'> {}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
2525
})
2626
}
2727

28+
const setTimeoutRef = (ref: NodeJS.Timeout) => {
29+
if (timeoutRef.current) {
30+
clearTimeout(timeoutRef.current)
31+
}
32+
timeoutRef.current = ref
33+
}
34+
2835
const checkConnectivity = async () => {
2936
if (isAirgapped) return
3037

@@ -33,20 +40,23 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
3340
abortControllerRef.current = controller
3441

3542
try {
36-
await getInternetConnectivity(abortControllerRef.current)
43+
await getInternetConnectivity({ controller: abortControllerRef.current, setTimeoutRef, checkConnectivity })
3744
onConnectivitySuccess()
38-
39-
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
4045
} catch (error) {
4146
if (getIsRequestAborted(error)) return
4247
const fallbackController = new AbortController()
4348
abortControllerRef.current = fallbackController
4449
try {
45-
await getFallbackInternetConnectivity(fallbackController)
50+
await getFallbackInternetConnectivity({
51+
controller: abortControllerRef.current,
52+
setTimeoutRef,
53+
checkConnectivity,
54+
})
4655
onConnectivitySuccess()
47-
} catch {
48-
setOnline(false)
49-
} finally {
56+
} catch (fallbackError) {
57+
if (!getIsRequestAborted(fallbackError)) {
58+
setOnline(false)
59+
}
5060
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
5161
}
5262
}

0 commit comments

Comments
 (0)