-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,85 @@ | ||
import { useEffect, useState } from 'react'; | ||
import * as React from 'react'; | ||
import * as isEqual from 'react-fast-compare'; | ||
import { off, on } from './util'; | ||
|
||
export interface BatterySensorState { | ||
const { useState, useEffect } = React; | ||
|
||
export interface BatteryState { | ||
charging: boolean; | ||
level: number; | ||
chargingTime: number; | ||
dischargingTime: number; | ||
level: number; | ||
} | ||
|
||
const useBattery = () => { | ||
const [state, setState] = useState({}); | ||
let mounted = true; | ||
let battery: any = null; | ||
|
||
const onChange = () => { | ||
const { charging, level, chargingTime, dischargingTime } = battery; | ||
setState({ | ||
charging, | ||
level, | ||
chargingTime, | ||
dischargingTime, | ||
}); | ||
}; | ||
interface BatteryManager extends Readonly<BatteryState>, EventTarget { | ||
onchargingchange: () => void; | ||
onchargingtimechange: () => void; | ||
ondischargingtimechange: () => void; | ||
onlevelchange: () => void; | ||
} | ||
|
||
interface NavigatorWithPossibleBattery extends Navigator { | ||
getBattery?: () => Promise<BatteryManager>; | ||
} | ||
|
||
type UseBatteryState = | ||
| { isSupported: false } // Battery API is not supported | ||
| { isSupported: true; fetched: false } // battery API supported but not fetched yet | ||
| BatteryState & { isSupported: true; fetched: true }; // battery API supported and fetched | ||
|
||
const onBattery = () => { | ||
onChange(); | ||
on(battery, 'chargingchange', onChange); | ||
on(battery, 'levelchange', onChange); | ||
on(battery, 'chargingtimechange', onChange); | ||
on(battery, 'dischargingtimechange', onChange); | ||
}; | ||
const nav: NavigatorWithPossibleBattery | undefined = typeof navigator === 'object' ? navigator : undefined; | ||
const isBatteryApiSupported = nav && typeof nav.getBattery === 'function'; | ||
|
||
function useBatteryMock(): UseBatteryState { | ||
return { isSupported: false }; | ||
} | ||
|
||
function useBattery(): UseBatteryState { | ||
const [state, setState] = useState<UseBatteryState>({ isSupported: true, fetched: false }); | ||
|
||
useEffect(() => { | ||
(navigator as any).getBattery().then((bat: any) => { | ||
if (mounted) { | ||
battery = bat; | ||
onBattery(); | ||
let isMounted = true; | ||
let battery: BatteryManager | null = null; | ||
|
||
const handleChange = () => { | ||
if (!isMounted || !battery) { | ||
return; | ||
} | ||
const newState: UseBatteryState = { | ||
isSupported: true, | ||
fetched: true, | ||
level: battery.level, | ||
charging: battery.charging, | ||
dischargingTime: battery.dischargingTime, | ||
chargingTime: battery.chargingTime, | ||
}; | ||
!isEqual(state, newState) && setState(newState); | ||
}; | ||
|
||
nav!.getBattery!().then((bat: BatteryManager) => { | ||
if (!isMounted) { | ||
return; | ||
} | ||
battery = bat; | ||
on(battery, 'chargingchange', handleChange); | ||
on(battery, 'chargingtimechange', handleChange); | ||
on(battery, 'dischargingtimechange', handleChange); | ||
on(battery, 'levelchange', handleChange); | ||
handleChange(); | ||
}); | ||
|
||
return () => { | ||
mounted = false; | ||
isMounted = false; | ||
if (battery) { | ||
off(battery, 'chargingchange', onChange); | ||
off(battery, 'levelchange', onChange); | ||
off(battery, 'chargingtimechange', onChange); | ||
off(battery, 'dischargingtimechange', onChange); | ||
off(battery, 'chargingchange', handleChange); | ||
off(battery, 'chargingtimechange', handleChange); | ||
off(battery, 'dischargingtimechange', handleChange); | ||
off(battery, 'levelchange', handleChange); | ||
} | ||
}; | ||
}, []); | ||
|
||
return state; | ||
}; | ||
} | ||
|
||
export default useBattery; | ||
export default isBatteryApiSupported ? useBattery : useBatteryMock; |