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