{"version":3,"sources":["../../src/use-network/index.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { useEventListener } from '../use-event-listener'\nimport { useSetState } from '../use-set-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useSupported } from '../use-supported'\nimport { now } from '../utils/basic'\n\nexport type NetworkType = 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown'\nexport type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined\n\nexport interface UseNetworkReturns {\n  /**\n   * Whether the browser supports the Network Information API.\n   */\n  isSupported: boolean\n  /**\n   * Whether the user is currently connected.\n   */\n  isOnline: boolean\n  /**\n   * The time since the user was last connected.\n   */\n  offlineAt: number | undefined\n  /**\n   * At this time, if the user is offline and reconnects.\n   */\n  onlineAt: number | undefined\n  /**\n   * download speed in Mbps.\n   */\n  downlink: number | undefined\n  /**\n   * The max reachable download speed in Mbps.\n   */\n  downlinkMax: number | undefined\n  /**\n   * The detected effective speed type.\n   */\n  effectiveType: NetworkEffectiveType | undefined\n  /**\n   * The estimated effective round-trip time of the current connection.\n   */\n  rtt: number | undefined\n  /**\n   * If the user activated data saver mode.\n   */\n  saveData: boolean | undefined\n  /**\n   * The detected connection/network type. Only supported in Web Worker in Chrome.\n   *\n   * @see https://developer.mozilla.org/zh-CN/docs/Web/API/NetworkInformation/type\n   */\n  type: NetworkType\n}\n\ninterface ExtendedNavigator extends Navigator {\n  connection?: {\n    downlink: number\n    downlinkMax: number\n    effectiveType: NetworkEffectiveType\n    rtt: number\n    saveData: boolean\n    type: NetworkType\n    addEventListener: (type: 'change', listener: () => void) => void\n    removeEventListener: (type: 'change', listener: () => void) => void\n    dispatchEvent: (event: Event) => boolean\n  }\n}\n\n/**\n * A React Hook that tracks network status.\n */\nexport function useNetwork(): UseNetworkReturns {\n  const isSupported = useSupported(() => 'connection' in navigator)\n  const connectionRef = useRef<ExtendedNavigator['connection'] | null>(null)\n\n  const [state, setState] = useSetState({\n    isOnline: true,\n    offlineAt: undefined as number | undefined,\n    onlineAt: 0 as number | undefined,\n    downlink: undefined as number | undefined,\n    downlinkMax: undefined as number | undefined,\n    effectiveType: undefined as NetworkEffectiveType,\n    rtt: undefined as number | undefined,\n    saveData: undefined as boolean | undefined,\n    type: 'unknown' as NetworkType,\n  })\n\n  const updateNetworkInformation = useStableFn(() => setState(getNetwork()))\n\n  useEffect(() => {\n    if (!isSupported) return\n    connectionRef.current = (navigator as ExtendedNavigator)?.connection\n    updateNetworkInformation()\n  }, [isSupported])\n\n  useEventListener(connectionRef, 'change', updateNetworkInformation, false)\n  useEventListener(() => window, ['offline', 'online'], updateNetworkInformation)\n\n  return { isSupported, ...state }\n}\n\nfunction getNetwork(): Omit<UseNetworkReturns, 'isSupported'> {\n  return {\n    isOnline: navigator.onLine,\n    offlineAt: navigator.onLine ? undefined : now(),\n    onlineAt: navigator.onLine ? now() : undefined,\n    downlink: (navigator as ExtendedNavigator)?.connection?.downlink,\n    downlinkMax: (navigator as ExtendedNavigator)?.connection?.downlinkMax,\n    effectiveType: (navigator as ExtendedNavigator)?.connection?.effectiveType,\n    rtt: (navigator as ExtendedNavigator)?.connection?.rtt,\n    saveData: (navigator as ExtendedNavigator)?.connection?.saveData,\n    type: (navigator as ExtendedNavigator)?.connection?.type ?? 'unknown',\n  }\n}\n"],"names":["useNetwork","isSupported","useSupported","navigator","connectionRef","useRef","useSetState","isOnline","offlineAt","undefined","onlineAt","downlink","downlinkMax","effectiveType","rtt","saveData","type","state","setState","updateNetworkInformation","useStableFn","getNetwork","useEffect","current","connection","useEventListener","window","onLine","now"],"mappings":";;;;+BAwEgBA;;;eAAAA;;;qBAxEkB;gCACD;2BACL;2BACA;4BACC;qBACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEb,SAASA;IACd,IAAMC,cAAcC,IAAAA,0BAAY,EAAC;eAAM,gBAAgBC;;I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