|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { isBrowser, noop } from './util/const'; |
| 3 | +import { IInitialState } from './util/resolveHookState'; |
| 4 | +import { useSafeState } from './useSafeState'; |
| 5 | +import { off, on } from './util/misc'; |
| 6 | + |
| 7 | +export interface INetworkInformation extends EventTarget { |
| 8 | + readonly downlink: number; |
| 9 | + readonly downlinkMax: number; |
| 10 | + readonly effectiveType: 'slow-2g' | '2g' | '3g' | '4g'; |
| 11 | + readonly rtt: number; |
| 12 | + readonly saveData: boolean; |
| 13 | + readonly type: |
| 14 | + | 'bluetooth' |
| 15 | + | 'cellular' |
| 16 | + | 'ethernet' |
| 17 | + | 'none' |
| 18 | + | 'wifi' |
| 19 | + | 'wimax' |
| 20 | + | 'other' |
| 21 | + | 'unknown'; |
| 22 | +} |
| 23 | + |
| 24 | +export interface IUseNetworkState { |
| 25 | + /** |
| 26 | + * @desc Whether browser connected to the network or not. |
| 27 | + */ |
| 28 | + online: boolean | undefined; |
| 29 | + /** |
| 30 | + * @desc Previous value of `online` property. Helps to identify if browser |
| 31 | + * just connected or lost connection. |
| 32 | + */ |
| 33 | + previous: boolean | undefined; |
| 34 | + /** |
| 35 | + * @desc The {Date} object pointing to the moment when state change occurred. |
| 36 | + */ |
| 37 | + since: Date | undefined; |
| 38 | + /** |
| 39 | + * @desc Effective bandwidth estimate in megabits per second, rounded to the |
| 40 | + * nearest multiple of 25 kilobits per seconds. |
| 41 | + */ |
| 42 | + downlink: INetworkInformation['downlink'] | undefined; |
| 43 | + /** |
| 44 | + * @desc Maximum downlink speed, in megabits per second (Mbps), for the |
| 45 | + * underlying connection technology |
| 46 | + */ |
| 47 | + downlinkMax: INetworkInformation['downlinkMax'] | undefined; |
| 48 | + /** |
| 49 | + * @desc Effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'. |
| 50 | + * This value is determined using a combination of recently observed round-trip time |
| 51 | + * and downlink values. |
| 52 | + */ |
| 53 | + effectiveType: INetworkInformation['effectiveType'] | undefined; |
| 54 | + /** |
| 55 | + * @desc Estimated effective round-trip time of the current connection, rounded |
| 56 | + * to the nearest multiple of 25 milliseconds |
| 57 | + */ |
| 58 | + rtt: INetworkInformation['rtt'] | undefined; |
| 59 | + /** |
| 60 | + * @desc {true} if the user has set a reduced data usage option on the user agent. |
| 61 | + */ |
| 62 | + saveData: INetworkInformation['saveData'] | undefined; |
| 63 | + /** |
| 64 | + * @desc The type of connection a device is using to communicate with the network. |
| 65 | + * It will be one of the following values: |
| 66 | + * - bluetooth |
| 67 | + * - cellular |
| 68 | + * - ethernet |
| 69 | + * - none |
| 70 | + * - wifi |
| 71 | + * - wimax |
| 72 | + * - other |
| 73 | + * - unknown |
| 74 | + */ |
| 75 | + type: INetworkInformation['type'] | undefined; |
| 76 | +} |
| 77 | + |
| 78 | +const navigator: |
| 79 | + | (Navigator & |
| 80 | + Partial<Record<'connection' | 'mozConnection' | 'webkitConnection', INetworkInformation>>) |
| 81 | + | undefined = isBrowser ? window.navigator : undefined; |
| 82 | + |
| 83 | +const conn: INetworkInformation | undefined = |
| 84 | + navigator && (navigator.connection || navigator.mozConnection || navigator.webkitConnection); |
| 85 | + |
| 86 | +function getConnectionState(previousState?: IUseNetworkState): IUseNetworkState { |
| 87 | + const online = navigator?.onLine; |
| 88 | + const previousOnline = previousState?.online; |
| 89 | + |
| 90 | + return { |
| 91 | + online, |
| 92 | + previous: previousOnline, |
| 93 | + since: online !== previousOnline ? new Date() : previousState?.since, |
| 94 | + downlink: conn?.downlink, |
| 95 | + downlinkMax: conn?.downlinkMax, |
| 96 | + effectiveType: conn?.effectiveType, |
| 97 | + rtt: conn?.rtt, |
| 98 | + saveData: conn?.saveData, |
| 99 | + type: conn?.type, |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Tracks the state of browser's network connection. |
| 105 | + */ |
| 106 | +export const useNetworkState: typeof navigator.connection extends undefined |
| 107 | + ? undefined |
| 108 | + : (initialState?: IInitialState<IUseNetworkState>) => IUseNetworkState = isBrowser |
| 109 | + ? function useNetworkState(initialState?: IInitialState<IUseNetworkState>): IUseNetworkState { |
| 110 | + const [state, setState] = useSafeState(initialState ?? getConnectionState); |
| 111 | + |
| 112 | + useEffect(() => { |
| 113 | + const handleStateChange = () => { |
| 114 | + setState(getConnectionState); |
| 115 | + }; |
| 116 | + |
| 117 | + on(window, 'online', handleStateChange, { passive: true }); |
| 118 | + on(window, 'offline', handleStateChange, { passive: true }); |
| 119 | + |
| 120 | + // it is quite hard to test it in jsdom environment maybe will be improved in future |
| 121 | + /* istanbul ignore next */ |
| 122 | + if (conn) { |
| 123 | + on(conn, 'change', handleStateChange, { passive: true }); |
| 124 | + } |
| 125 | + |
| 126 | + return () => { |
| 127 | + off(window, 'online', handleStateChange); |
| 128 | + off(window, 'offline', handleStateChange); |
| 129 | + |
| 130 | + /* istanbul ignore next */ |
| 131 | + if (conn) { |
| 132 | + off(conn, 'change', handleStateChange); |
| 133 | + } |
| 134 | + }; |
| 135 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 136 | + }, []); |
| 137 | + |
| 138 | + return state; |
| 139 | + } |
| 140 | + : (noop as () => undefined); |
0 commit comments