Skip to content

Commit

Permalink
fix: use powerMonitor.on() only after app is ready
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ckerr committed Jan 28, 2020
1 parent 55ccca9 commit 2f2bd8d
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/browser/api/power-monitor.ts
Expand Up @@ -10,18 +10,28 @@ Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)

const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true)

// On Linux we need to call blockShutdown() to subscribe to shutdown event.
if (process.platform === 'linux') {
powerMonitor.on('newListener', (event:string) => {
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.blockShutdown()
}
})

powerMonitor.on('removeListener', (event: string) => {
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.unblockShutdown()
}
/* In order to delay system shutdown when e.preventDefault() is invoked
* on a powerMonitor 'shutdown' event, we need an org.freedesktop.login1
* 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.once('ready', (): void => {

This comment has been minimized.

Copy link
@vladimiry

vladimiry Jan 28, 2020

Will the ready event be replayed if it got fired already before this code starts? I mean power-monitor module could be loaded lazily after ready got fired already and the guess is ready event won't be replayed in this case. This is why I was thinking about using/introducing something like app.promises.ready (the promise gets resolved by app module in along with firing the ready event).

powerMonitor.on('newListener', (event: string) => {
// if first shutdown listener added
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.blockShutdown()
}
})
powerMonitor.on('removeListener', (event: string) => {
// if last shutdown listener removed
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
powerMonitor.unblockShutdown()
}
})
})
}

Expand Down

0 comments on commit 2f2bd8d

Please sign in to comment.