📄 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
Calls:
useSupported (from ../useSupported)shallowRef (from vue)(navigator as NavigatorWithBattery) .getBattery() .thenupdateBatteryInfo.calluseEventListener (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:
thisBatteryManager
Returns: void
Code
Interfaces¶
UseBatteryOptions¶
UseBatteryReturn¶
Interface Code
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
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
charging |
boolean |
✗ | not shown |
chargingTime |
number |
✗ | not shown |
dischargingTime |
number |
✗ | not shown |
level |
number |
✗ | not shown |
Type Aliases¶
NavigatorWithBattery¶
Generated by Syntax Scribe