2020-03-20 20:28:31 +00:00
|
|
|
'use strict';
|
2018-09-22 12:28:50 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
import { createLazyInstance } from '../utils';
|
2019-04-30 00:46:08 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-02 04:05:27 +00:00
|
|
|
// PowerMonitor is an EventEmitter.
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype);
|
2019-04-30 00:46:08 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-02-05 06:28:58 +00:00
|
|
|
if (process.platform === 'linux') {
|
2020-01-28 18:19:44 +00:00
|
|
|
// 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.
|
2020-03-20 20:28:31 +00:00
|
|
|
const { app } = require('electron');
|
2020-01-28 18:19:44 +00:00
|
|
|
app.whenReady().then(() => {
|
|
|
|
powerMonitor.on('newListener', (event: string) => {
|
|
|
|
// whenever the listener count is incremented to one...
|
|
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
powerMonitor.blockShutdown();
|
2020-01-28 18:19:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-01-28 18:19:44 +00:00
|
|
|
powerMonitor.on('removeListener', (event: string) => {
|
|
|
|
// whenever the listener count is decremented to zero...
|
|
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
powerMonitor.unblockShutdown();
|
2020-01-28 18:19:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2018-02-05 06:28:58 +00:00
|
|
|
}
|
2017-12-07 13:10:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
module.exports = powerMonitor;
|