electron/lib/browser/api/power-monitor.ts

29 lines
889 B
TypeScript
Raw Normal View History

'use strict'
import { createLazyInstance } from '../utils'
2018-09-13 16:10:51 +00:00
const { EventEmitter } = require('events')
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor')
2016-01-12 02:40:23 +00:00
// PowerMonitor is an EventEmitter.
2016-08-02 11:38:35 +00:00
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true)
2016-01-12 02:40:23 +00:00
// 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()
}
})
}
module.exports = powerMonitor