|
| 1 | +import { reactive, onMounted, onUnmounted } from '@vue/composition-api' |
| 2 | +import { |
| 3 | + checkBrowser, |
| 4 | + addEventListener, |
| 5 | + removeEventListener, |
| 6 | + warn |
| 7 | +} from '../../../utils' |
| 8 | + |
| 9 | +export interface BatteryState { |
| 10 | + charging: boolean |
| 11 | + chargingTime: number |
| 12 | + dischargingTime: number |
| 13 | + level: number |
| 14 | +} |
| 15 | + |
| 16 | +type FetchedBatteryState = BatteryState & { |
| 17 | + isSupported: boolean |
| 18 | + fetched: boolean |
| 19 | +} |
| 20 | + |
| 21 | +type UseBatteryState = |
| 22 | + | { isSupported: false } // Battery API is not supported |
| 23 | + | FetchedBatteryState // battery API supported |
| 24 | + |
| 25 | +export interface BatteryManager extends Readonly<BatteryState>, EventTarget { |
| 26 | + onchargingchange: VoidFunction |
| 27 | + onchargingtimechange: VoidFunction |
| 28 | + ondischargingtimechange: VoidFunction |
| 29 | + onlevelchange: VoidFunction |
| 30 | +} |
| 31 | + |
| 32 | +interface NavigatorWithPossibleBattery extends Navigator { |
| 33 | + getBattery?: () => Promise<BatteryManager> |
| 34 | +} |
| 35 | + |
| 36 | +const nav: NavigatorWithPossibleBattery | undefined = |
| 37 | + typeof navigator === 'object' ? navigator : undefined |
| 38 | +const isBatteryApiSupported = nav && typeof nav.getBattery === 'function' |
| 39 | + |
| 40 | +export default function useBattery(): UseBatteryState { |
| 41 | + checkBrowser(useBattery.name) |
| 42 | + |
| 43 | + if (!isBatteryApiSupported) { |
| 44 | + return { isSupported: false } |
| 45 | + } |
| 46 | + |
| 47 | + const state = reactive<FetchedBatteryState>({ |
| 48 | + isSupported: true, |
| 49 | + fetched: false, |
| 50 | + charging: false, |
| 51 | + chargingTime: 0, |
| 52 | + dischargingTime: 0, |
| 53 | + level: 0 |
| 54 | + }) |
| 55 | + |
| 56 | + let mounted = true |
| 57 | + let battery: BatteryManager | null = null |
| 58 | + |
| 59 | + const update = () => { |
| 60 | + if (!mounted || !battery) { |
| 61 | + return |
| 62 | + } |
| 63 | + state.isSupported = true |
| 64 | + state.fetched = true |
| 65 | + state.level = battery.level |
| 66 | + state.charging = battery.charging |
| 67 | + state.dischargingTime = battery.dischargingTime |
| 68 | + state.chargingTime = battery.chargingTime |
| 69 | + } |
| 70 | + |
| 71 | + onMounted(() => { |
| 72 | + nav!.getBattery!() |
| 73 | + .then((bm: BatteryManager) => { |
| 74 | + if (!mounted) return |
| 75 | + battery = bm |
| 76 | + addEventListener(battery, 'chargingchange', update) |
| 77 | + addEventListener(battery, 'chargingtimechange', update) |
| 78 | + addEventListener(battery, 'dischargingtimechange', update) |
| 79 | + addEventListener(battery, 'levelchange', update) |
| 80 | + update() |
| 81 | + }) |
| 82 | + .catch(warn) |
| 83 | + }) |
| 84 | + |
| 85 | + onUnmounted(() => { |
| 86 | + mounted = false |
| 87 | + if (battery) { |
| 88 | + removeEventListener(battery, 'chargingchange', update) |
| 89 | + removeEventListener(battery, 'chargingtimechange', update) |
| 90 | + removeEventListener(battery, 'dischargingtimechange', update) |
| 91 | + removeEventListener(battery, 'levelchange', update) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + return state |
| 96 | +} |
0 commit comments