2020-03-24 16:03:29 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2020-07-13 16:58:49 +00:00
|
|
|
import { app } from 'electron/main';
|
2018-09-22 12:28:50 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const {
|
|
|
|
createPowerMonitor,
|
|
|
|
getSystemIdleState,
|
|
|
|
getSystemIdleTime
|
|
|
|
} = process._linkedBinding('electron_browser_power_monitor');
|
2019-04-30 00:46:08 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
class PowerMonitor extends EventEmitter {
|
|
|
|
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', () => {
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
const pm = createPowerMonitor();
|
|
|
|
pm.emit = this.emit.bind(this);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-04-30 00:46:08 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
getSystemIdleState (idleThreshold: number) {
|
|
|
|
return getSystemIdleState(idleThreshold);
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
getSystemIdleTime () {
|
|
|
|
return getSystemIdleTime();
|
|
|
|
}
|
2018-02-05 06:28:58 +00:00
|
|
|
}
|
2017-12-07 13:10:40 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
module.exports = new PowerMonitor();
|