Skip to content

⬅️ Back to Table of Contents

📄 useBattery

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 7
⚡ Async/Await Patterns 1
📐 Interfaces 3
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useBattery/index.ts

📦 Imports

Name Source
ShallowRef vue
ConfigurableNavigator ../_configurable
Supportable ../types
shallowRef vue
defaultNavigator ../_configurable
useEventListener ../useEventListener
useSupported ../useSupported

Async/Await Patterns

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

Functions

useBattery(options: UseBatteryOptions): UseBatteryReturn

Reactive Battery Status API.

See: https://vueuse.org/useBattery

Tags: @__NO_SIDE_EFFECTS__

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

Calls:

  • useSupported (from ../useSupported)
  • shallowRef (from vue)
  • (navigator as NavigatorWithBattery) .getBattery() .then
  • updateBatteryInfo.call
  • useEventListener (from ../useEventListener)
Code
export function useBattery(options: UseBatteryOptions = {}): UseBatteryReturn {
  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,
  }
}

Internal helpers

Declared inside another function in this file.

updateBatteryInfo(this: BatteryManager): void

Parameters:

  • this BatteryManager

Returns: void

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

Interfaces

UseBatteryOptions

Interface Code
export interface UseBatteryOptions extends ConfigurableNavigator {
}

UseBatteryReturn

Interface Code
export interface UseBatteryReturn extends Supportable {
  charging: ShallowRef<boolean>
  chargingTime: ShallowRef<number>
  dischargingTime: ShallowRef<number>
  level: ShallowRef<number>
}

Properties

Name Type Optional Description
charging ShallowRef<boolean> not shown
chargingTime ShallowRef<number> not shown
dischargingTime ShallowRef<number> not shown
level ShallowRef<number> not shown

BatteryManager

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

Properties

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

Type Aliases

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

Generated by Syntax Scribe