{"version":3,"sources":["../../src/use-battery/index.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { useAsyncEffect } from '../use-async-effect'\nimport { useAsyncLock } from '../use-async-lock'\nimport { useEventListener } from '../use-event-listener'\nimport { useSafeState } from '../use-safe-state'\nimport { useSupported } from '../use-supported'\n\nexport interface UseBatteryReturns {\n  /**\n   * Whether the battery is charging\n   */\n  charging: boolean\n  /**\n   * The battery level as a floating point number between 0 and 1\n   */\n  level: number\n  /**\n   * The time remaining to charge the battery fully\n   */\n  chargingTime: number\n  /**\n   * The time remaining to discharge the battery fully\n   */\n  dischargingTime: number\n  /**\n   * Whether the battery API is supported\n   */\n  isSupported: boolean\n  /**\n   * Update the battery state\n   */\n  update(): void\n}\n\ninterface BatteryManager extends EventTarget {\n  /**\n   * Whether the battery is charging\n   */\n  charging: boolean\n  /**\n   * The time remaining to charge the battery fully\n   */\n  chargingTime: number\n  /**\n   * The time remaining to discharge the battery fully\n   */\n  dischargingTime: number\n  /**\n   * The battery level as a floating point number between 0 and 1\n   */\n  level: number\n}\n\ninterface NavigatorWithBattery extends Navigator {\n  /**\n   * Get the battery manager\n   */\n  getBattery(): Promise<BatteryManager>\n}\n\nconst batteryEvents = ['chargingchange', 'chargingtimechange', 'dischargingtimechange', 'levelchange']\n\n/**\n * A React Hook that get [Battery](https://developer.mozilla.org/zh-CN/docs/Web/API/Battery_Status_API) state with ease.\n *\n * @returns {UseBatteryReturns} `UseBatteryReturns`, see {@link UseBatteryReturns}\n * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/Battery_Status_API | Battery Status API - MDN}\n */\nexport function useBattery(): UseBatteryReturns {\n  const isSupported = useSupported(() => 'getBattery' in navigator)\n\n  const [state, setState] = useSafeState(\n    {\n      charging: false,\n      chargingTime: 0,\n      dischargingTime: 0,\n      level: 1,\n    },\n    { deep: true },\n  )\n\n  const bmRef = useRef<BatteryManager | null>(null)\n\n  async function getBatteryState() {\n    bmRef.current = await (navigator as NavigatorWithBattery).getBattery()\n\n    return {\n      charging: bmRef.current.charging ?? false,\n      chargingTime: bmRef.current.chargingTime ?? 0,\n      dischargingTime: bmRef.current.dischargingTime ?? 0,\n      level: bmRef.current.level ?? 0,\n    }\n  }\n\n  const updateBatteryState = useAsyncLock(async () => setState(await getBatteryState()))\n\n  useAsyncEffect(async () => {\n    if (!isSupported) return\n    await updateBatteryState()\n  }, [isSupported])\n\n  useEventListener(isSupported ? bmRef : null, batteryEvents, updateBatteryState, { passive: true })\n\n  return {\n    isSupported,\n    update: updateBatteryState,\n    ...state,\n  }\n}\n"],"names":["useBattery","batteryEvents","isSupported","useSupported","navigator","useSafeState","charging","chargingTime","dischargingTime","level","deep","state","setState","bmRef","useRef","getBatteryState","getBattery","current","updateBatteryState","useAsyncLock","useAsyncEffect","useEventListener","passive","update"],"mappings":";;;;+BAoEgBA;;;eAAAA;;;qBApEO;8BACQ;4BACF;gCACI;4BACJ;4BACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuD7B,IAAMC,gBAAgB;IAAC;IAAkB;IAAsB;IAAyB;CAAc;AAQ/F,SAASD;IACd,IAAME,cAAcC,IAAAA,0BAAY,EAAC;eAAM,gBAAgBC;;IAEvD,IAA0BC,iCAAAA,IAAAA,0BAAY,EACpC;QACEC,UAAU;QACVC,cAAc;QACdC,iBAAiB;QACjBC,OAAO;IACT,GACA;QAAEC,MAAM;IAAK,QAPRC,QAAmBN,kBAAZO,WAAYP;IAU1B,IAAMQ,QAAQC,IAAAA,aAAM,EAAwB;IAE5C,SAAeC;;gBAIDF,yBACIA,6BACGA,gCACVA;;;;wBANO;;4BAAOT,UAAmCY,UAAU;;;wBAApEH,M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