-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: replace the old version with a more maintainable Network Reach…
…ability test. (#6392)
- Loading branch information
1 parent
2214d4a
commit 95f5391
Showing
16 changed files
with
287 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useMemo } from 'react'; | ||
|
||
enum EDeferStatus { | ||
pending = 'pending', | ||
resolved = 'resolved', | ||
rejected = 'rejected', | ||
} | ||
export type IDeferredPromise<DeferType> = { | ||
resolve: (value: DeferType) => void; | ||
reject: (value: unknown) => void; | ||
reset: () => void; | ||
promise: Promise<DeferType>; | ||
status: EDeferStatus; | ||
}; | ||
|
||
export const buildDeferredPromise = <DeferType>() => { | ||
const deferred = {} as IDeferredPromise<DeferType>; | ||
|
||
const buildPromise = () => { | ||
const promise = new Promise<DeferType>((resolve, reject) => { | ||
deferred.status = EDeferStatus.pending; | ||
deferred.resolve = (value: DeferType) => { | ||
deferred.status = EDeferStatus.resolved; | ||
resolve(value); | ||
}; | ||
deferred.reject = (reason: unknown) => { | ||
deferred.status = EDeferStatus.rejected; | ||
reject(reason); | ||
}; | ||
}); | ||
|
||
deferred.promise = promise; | ||
}; | ||
|
||
buildPromise(); | ||
|
||
deferred.reset = () => { | ||
if (deferred.status !== EDeferStatus.pending) { | ||
buildPromise(); | ||
} | ||
}; | ||
return deferred; | ||
}; | ||
|
||
export function useDeferredPromise<DeferType>() { | ||
const defer = useMemo(() => buildDeferredPromise<DeferType>(), []); | ||
return defer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { buildDeferredPromise } from './useDeferredPromise'; | ||
import { | ||
getCurrentVisibilityState, | ||
onVisibilityStateChange, | ||
} from './useVisibilityChange'; | ||
|
||
export interface IReachabilityConfiguration { | ||
reachabilityUrl: string; | ||
reachabilityTest?: (response: { status: number }) => Promise<boolean>; | ||
reachabilityMethod?: 'GET' | 'POST'; | ||
reachabilityLongTimeout?: number; | ||
reachabilityShortTimeout?: number; | ||
reachabilityRequestTimeout?: number; | ||
} | ||
|
||
export interface IReachabilityState { | ||
isInternetReachable: boolean | null; | ||
} | ||
|
||
class NetInfo { | ||
state: IReachabilityState = { | ||
isInternetReachable: null, | ||
}; | ||
|
||
prevIsInternetReachable = false; | ||
|
||
listeners: Array<(state: { isInternetReachable: boolean | null }) => void> = | ||
[]; | ||
|
||
defer = buildDeferredPromise<unknown>(); | ||
|
||
configuration = { | ||
reachabilityUrl: '', | ||
reachabilityMethod: 'GET', | ||
reachabilityTest: (response: { status: number }) => | ||
Promise.resolve(response.status === 200), | ||
reachabilityLongTimeout: 60 * 1000, | ||
reachabilityShortTimeout: 5 * 1000, | ||
reachabilityRequestTimeout: 10 * 1000, | ||
}; | ||
|
||
isFetching = false; | ||
|
||
pollingTimeoutId: ReturnType<typeof setTimeout> | null = null; | ||
|
||
constructor(configuration: IReachabilityConfiguration) { | ||
this.configure(configuration); | ||
|
||
const handleVisibilityChange = (isVisible: boolean) => { | ||
if (isVisible) { | ||
this.defer.resolve(undefined); | ||
} else { | ||
this.defer.reset(); | ||
} | ||
}; | ||
|
||
const isVisible = getCurrentVisibilityState(); | ||
handleVisibilityChange(isVisible); | ||
onVisibilityStateChange(handleVisibilityChange); | ||
} | ||
|
||
configure(configuration: IReachabilityConfiguration) { | ||
this.configuration = { | ||
...this.configuration, | ||
...configuration, | ||
}; | ||
} | ||
|
||
currentState() { | ||
return this.state; | ||
} | ||
|
||
updateState(state: { isInternetReachable: boolean | null }) { | ||
this.state = state; | ||
this.listeners.forEach((listener) => listener(state)); | ||
this.prevIsInternetReachable = !!state.isInternetReachable; | ||
} | ||
|
||
addEventListener( | ||
listener: (state: { isInternetReachable: boolean | null }) => void, | ||
) { | ||
this.listeners.push(listener); | ||
return () => { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
}; | ||
} | ||
|
||
async fetch() { | ||
if (this.isFetching) return; | ||
this.isFetching = true; | ||
await this.defer.promise; | ||
|
||
const { | ||
reachabilityRequestTimeout, | ||
reachabilityUrl, | ||
reachabilityMethod, | ||
reachabilityTest, | ||
} = this.configuration; | ||
|
||
const controller = new AbortController(); | ||
const timeoutId = setTimeout( | ||
() => controller.abort(), | ||
reachabilityRequestTimeout, | ||
); | ||
|
||
try { | ||
const response = await fetch(reachabilityUrl, { | ||
method: reachabilityMethod, | ||
signal: controller.signal, | ||
}); | ||
|
||
this.updateState({ | ||
isInternetReachable: await reachabilityTest(response), | ||
}); | ||
} catch (error) { | ||
console.error('Failed to fetch reachability:', error); | ||
this.updateState({ isInternetReachable: false }); | ||
} finally { | ||
clearTimeout(timeoutId); | ||
this.isFetching = false; | ||
const { reachabilityShortTimeout, reachabilityLongTimeout } = | ||
this.configuration; | ||
this.pollingTimeoutId = setTimeout( | ||
() => { | ||
void this.fetch(); | ||
}, | ||
this.prevIsInternetReachable | ||
? reachabilityLongTimeout | ||
: reachabilityShortTimeout, | ||
); | ||
} | ||
} | ||
|
||
async start() { | ||
void this.fetch(); | ||
} | ||
|
||
async refresh() { | ||
if (this.pollingTimeoutId) { | ||
clearTimeout(this.pollingTimeoutId); | ||
} | ||
void this.fetch(); | ||
} | ||
} | ||
|
||
export const globalNetInfo = new NetInfo({ | ||
reachabilityUrl: '/wallet/v1/health', | ||
}); | ||
|
||
export const configureNetInfo = (configuration: IReachabilityConfiguration) => { | ||
globalNetInfo.configure(configuration); | ||
void globalNetInfo.start(); | ||
}; | ||
|
||
export const refreshNetInfo = () => { | ||
void globalNetInfo.refresh(); | ||
}; | ||
|
||
export const useNetInfo = () => { | ||
const [reachabilityState, setReachabilityState] = useState< | ||
IReachabilityState & { | ||
isRawInternetReachable: boolean | null; | ||
} | ||
>(() => { | ||
const { isInternetReachable } = globalNetInfo.currentState(); | ||
return { | ||
isInternetReachable: isInternetReachable ?? true, | ||
isRawInternetReachable: isInternetReachable, | ||
}; | ||
}); | ||
useEffect(() => { | ||
const remove = globalNetInfo.addEventListener(({ isInternetReachable }) => { | ||
setReachabilityState({ | ||
isInternetReachable: isInternetReachable ?? true, | ||
isRawInternetReachable: isInternetReachable, | ||
}); | ||
}); | ||
return remove; | ||
}, []); | ||
return reachabilityState; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.