2020-03-24 10:03:29 -06:00
|
|
|
import { EventEmitter } from 'events';
|
2018-09-22 06:28:50 -06:00
|
|
|
|
2020-06-22 21:32:45 -06:00
|
|
|
const {
|
|
|
|
createPowerMonitor,
|
|
|
|
getSystemIdleState,
|
2020-11-16 17:31:46 -07:00
|
|
|
getSystemIdleTime,
|
2023-04-19 03:46:55 -06:00
|
|
|
getCurrentThermalState,
|
2020-11-16 17:31:46 -07:00
|
|
|
isOnBatteryPower
|
2020-06-22 21:32:45 -06:00
|
|
|
} = process._linkedBinding('electron_browser_power_monitor');
|
2019-04-29 18:46:08 -06:00
|
|
|
|
2023-10-25 12:02:15 -06:00
|
|
|
class PowerMonitor extends EventEmitter implements Electron.PowerMonitor {
|
2020-03-24 10:03:29 -06:00
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
// Don't start the event source until both a) the app is ready and b)
|
|
|
|
// there's a listener registered for a powerMonitor event.
|
|
|
|
this.once('newListener', () => {
|
2023-04-12 16:37:52 -06:00
|
|
|
const pm = createPowerMonitor();
|
|
|
|
pm.emit = this.emit.bind(this);
|
|
|
|
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
// On Linux, we inhibit shutdown in order to give the app a chance to
|
|
|
|
// decide whether or not it wants to prevent the shutdown. We don't
|
|
|
|
// inhibit the shutdown event unless there's a listener for it. This
|
|
|
|
// keeps the C++ code informed about whether there are any listeners.
|
|
|
|
pm.setListeningForShutdown(this.listenerCount('shutdown') > 0);
|
|
|
|
this.on('newListener', (event) => {
|
|
|
|
if (event === 'shutdown') {
|
|
|
|
pm.setListeningForShutdown(this.listenerCount('shutdown') + 1 > 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on('removeListener', (event) => {
|
|
|
|
if (event === 'shutdown') {
|
|
|
|
pm.setListeningForShutdown(this.listenerCount('shutdown') > 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-03-24 10:03:29 -06:00
|
|
|
});
|
|
|
|
}
|
2019-04-29 18:46:08 -06:00
|
|
|
|
2020-03-24 10:03:29 -06:00
|
|
|
getSystemIdleState (idleThreshold: number) {
|
|
|
|
return getSystemIdleState(idleThreshold);
|
|
|
|
}
|
2016-01-11 19:40:23 -07:00
|
|
|
|
2023-04-19 03:46:55 -06:00
|
|
|
getCurrentThermalState () {
|
|
|
|
return getCurrentThermalState();
|
|
|
|
}
|
|
|
|
|
2020-03-24 10:03:29 -06:00
|
|
|
getSystemIdleTime () {
|
|
|
|
return getSystemIdleTime();
|
|
|
|
}
|
2020-11-16 17:31:46 -07:00
|
|
|
|
|
|
|
isOnBatteryPower () {
|
|
|
|
return isOnBatteryPower();
|
|
|
|
}
|
|
|
|
|
|
|
|
get onBatteryPower () {
|
|
|
|
return this.isOnBatteryPower();
|
|
|
|
}
|
2018-02-04 23:28:58 -07:00
|
|
|
}
|
2017-12-07 06:10:40 -07:00
|
|
|
|
2020-03-24 10:03:29 -06:00
|
|
|
module.exports = new PowerMonitor();
|