fix: use powerMonitor.on() only after app is ready (#21927)
* fix: use powerMonitor.on() only after app is ready powerMonitor can't be used until the app is ready; however, on Linux, powerMonitor.on() was called as soon as lib/browser/api/power-monitor.ts was loaded. This patch takes @vladimiry's suggestion of wrapping that in an app.on('ready') handler to prevent powerMonitor.on() from being called prematurely. Fixes #21716
This commit is contained in:
parent
64f484c894
commit
1b4eb0b679
1 changed files with 21 additions and 11 deletions
|
@ -10,19 +10,29 @@ Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
||||||
|
|
||||||
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true)
|
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true)
|
||||||
|
|
||||||
// On Linux we need to call blockShutdown() to subscribe to shutdown event.
|
|
||||||
if (process.platform === 'linux') {
|
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) => {
|
powerMonitor.on('newListener', (event: string) => {
|
||||||
|
// whenever the listener count is incremented to one...
|
||||||
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
||||||
powerMonitor.blockShutdown()
|
powerMonitor.blockShutdown()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
powerMonitor.on('removeListener', (event: string) => {
|
powerMonitor.on('removeListener', (event: string) => {
|
||||||
|
// whenever the listener count is decremented to zero...
|
||||||
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
||||||
powerMonitor.unblockShutdown()
|
powerMonitor.unblockShutdown()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = powerMonitor
|
module.exports = powerMonitor
|
||||||
|
|
Loading…
Add table
Reference in a new issue