refactor: ginify powerMonitor (#22751)
This commit is contained in:
parent
aeae0d47bd
commit
07cd70a37e
10 changed files with 216 additions and 176 deletions
|
@ -1,38 +1,46 @@
|
|||
'use strict';
|
||||
import { EventEmitter } from 'events';
|
||||
import { app } from 'electron';
|
||||
|
||||
import { createLazyInstance } from '../utils';
|
||||
const { createPowerMonitor, getSystemIdleState, getSystemIdleTime } = process.electronBinding('power_monitor');
|
||||
|
||||
const { EventEmitter } = require('events');
|
||||
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor');
|
||||
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);
|
||||
|
||||
// PowerMonitor is an EventEmitter.
|
||||
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype);
|
||||
|
||||
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true);
|
||||
|
||||
if (process.platform === 'linux') {
|
||||
// In order to delay system shutdown when e.preventDefault() is invoked
|
||||
// on a powerMonitor 'shutdown' event, we need an org.freedesktop.login1
|
||||
// shutdown delay lock. For more details see the "Taking Delay Locks"
|
||||
// section of https://www.freedesktop.org/wiki/Software/systemd/inhibit/
|
||||
//
|
||||
// So here we watch for 'shutdown' listeners to be added or removed and
|
||||
// set or unset our shutdown delay lock accordingly.
|
||||
const { app } = require('electron');
|
||||
app.whenReady().then(() => {
|
||||
powerMonitor.on('newListener', (event: string) => {
|
||||
// whenever the listener count is incremented to one...
|
||||
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
||||
powerMonitor.blockShutdown();
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
powerMonitor.on('removeListener', (event: string) => {
|
||||
// whenever the listener count is decremented to zero...
|
||||
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
||||
powerMonitor.unblockShutdown();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getSystemIdleState (idleThreshold: number) {
|
||||
return getSystemIdleState(idleThreshold);
|
||||
}
|
||||
|
||||
getSystemIdleTime () {
|
||||
return getSystemIdleTime();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = powerMonitor;
|
||||
module.exports = new PowerMonitor();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue