@@ -12,13 +12,17 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
12
12
const timeoutRef = useRef < NodeJS . Timeout > ( null )
13
13
const { isAirgapped } = useMainContext ( )
14
14
15
+ const handleClearTimeout = ( ) => {
16
+ if ( timeoutRef . current ) {
17
+ clearTimeout ( timeoutRef . current )
18
+ }
19
+ abortControllerRef . current . abort ( )
20
+ }
21
+
15
22
const checkConnectivity = async ( ) => {
16
23
if ( isAirgapped ) return
17
- // Cancel any pending request
18
- if ( abortControllerRef . current ) {
19
- abortControllerRef . current . abort ( )
20
- }
21
24
25
+ handleClearTimeout ( )
22
26
const controller = new AbortController ( )
23
27
abortControllerRef . current = controller
24
28
@@ -39,10 +43,7 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
39
43
}
40
44
}
41
45
const handleOffline = ( ) => {
42
- if ( timeoutRef . current ) {
43
- clearTimeout ( timeoutRef . current )
44
- }
45
- abortControllerRef . current . abort ( )
46
+ handleClearTimeout ( )
46
47
setOnline ( false )
47
48
}
48
49
@@ -51,6 +52,11 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
51
52
await checkConnectivity ( )
52
53
}
53
54
55
+ useEffect ( ( ) => {
56
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
57
+ handleOnline ( )
58
+ } , [ ] )
59
+
54
60
useEffect ( ( ) => {
55
61
if ( isAirgapped ) return null
56
62
window . addEventListener ( 'online' , handleOnline )
0 commit comments