Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 48x 48x 48x 48x 48x 126x 129x | import { NEVER, Observable, distinctUntilChanged, fromEvent, map, merge, shareReplay, startWith } from 'rxjs'; export enum ConnectionStatus { down = 0, up } export type ConnectionStatusTracker = Observable<ConnectionStatus>; export interface ConnectionStatusTrackerInternals { isNodeEnv?: boolean; online$?: Observable<unknown>; offline$?: Observable<unknown>; initialStatus?: boolean; } /** * Returns an observable that emits the online status of the browser. * When running in Node, it always emits 'up' * * @returns {ConnectionStatusTracker} ConnectionStatusTracker */ export const createSimpleConnectionStatusTracker = ({ isNodeEnv = typeof window === 'undefined', online$ = isNodeEnv ? NEVER : fromEvent(window, 'online'), offline$ = isNodeEnv ? NEVER : fromEvent(window, 'offline'), initialStatus = isNodeEnv ? true : navigator.onLine }: ConnectionStatusTrackerInternals = {}): ConnectionStatusTracker => merge(online$.pipe(map(() => true)), offline$.pipe(map(() => false))).pipe( startWith(initialStatus), map((onLine) => (onLine ? ConnectionStatus.up : ConnectionStatus.down)), distinctUntilChanged(), shareReplay(1) ); |