Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 5
📊 Variables & Constants 2
⚡ Async/Await Patterns 1
📐 Interfaces 1
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useBattery/index.ts

📦 Imports

Name Source
ConfigurableNavigator ../_configurable
shallowRef vue
defaultNavigator ../_configurable
useEventListener ../useEventListener
useSupported ../useSupported

Variables & Constants

Name Type Kind Value Exported
events string[] const ['chargingchange', 'chargingtimechange', 'dischargingtimechange', 'levelchange']
battery BatteryManager | null let/var *not shown*

Async/Await Patterns

Type Function Await Expressions Promise Chains
promise-chain useBattery none (navigator as NavigatorWithBattery)
.getBattery().then

Functions

useBattery(options: ConfigurableNavigator): { isSupported: any; charging: any; chargingTime: any; dischargingTime: any; level: any; }

Code
export function useBattery(options: ConfigurableNavigator = {}) {
  const { navigator = defaultNavigator } = options
  const events = ['chargingchange', 'chargingtimechange', 'dischargingtimechange', 'levelchange']

  const isSupported = useSupported(() => navigator && 'getBattery' in navigator && typeof navigator.getBattery === 'function')

  const charging = shallowRef(false)
  const chargingTime = shallowRef(0)
  const dischargingTime = shallowRef(0)
  const level = shallowRef(1)

  let battery: BatteryManager | null

  function updateBatteryInfo(this: BatteryManager) {
    charging.value = this.charging
    chargingTime.value = this.chargingTime || 0
    dischargingTime.value = this.dischargingTime || 0
    level.value = this.level
  }

  if (isSupported.value) {
    (navigator as NavigatorWithBattery)
      .getBattery()
      .then((_battery) => {
        battery = _battery
        updateBatteryInfo.call(battery)
        useEventListener(battery, events, updateBatteryInfo, { passive: true })
      })
  }

  return {
    isSupported,
    charging,
    chargingTime,
    dischargingTime,
    level,
  }
}
  • JSDoc:

    /**
     * Reactive Battery Status API.
     *
     * @see https://vueuse.org/useBattery
     */
    

  • Parameters:

  • options: ConfigurableNavigator
  • Return Type: { isSupported: any; charging: any; chargingTime: any; dischargingTime: any; level: any; }
  • Calls:
  • useSupported (from ../useSupported)
  • shallowRef (from vue)
  • (navigator as NavigatorWithBattery) .getBattery() .then
  • updateBatteryInfo.call
  • useEventListener (from ../useEventListener)

updateBatteryInfo(this: BatteryManager): void

Code
function updateBatteryInfo(this: BatteryManager) {
    charging.value = this.charging
    chargingTime.value = this.chargingTime || 0
    dischargingTime.value = this.dischargingTime || 0
    level.value = this.level
  }
  • Parameters:
  • this: BatteryManager
  • Return Type: void

Interfaces

BatteryManager

Interface Code
export interface BatteryManager extends EventTarget {
  charging: boolean
  chargingTime: number
  dischargingTime: number
  level: number
}

Properties

Name Type Optional Description
charging boolean
chargingTime number
dischargingTime number
level number

Type Aliases

type NavigatorWithBattery = Navigator & {
  getBattery: () => Promise<BatteryManager>
};

UseBatteryReturn

type UseBatteryReturn = ReturnType<typeof useBattery>;