2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-04-30 00:46:08 +00:00
|
|
|
import { createLazyInstance } from '../utils'
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { EventEmitter } = require('events')
|
2019-04-30 00:46:08 +00:00
|
|
|
const { createPowerMonitor, PowerMonitor } = process.electronBinding('power_monitor')
|
2019-02-27 20:54:01 +00:00
|
|
|
const { deprecate } = require('electron')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-02 04:05:27 +00:00
|
|
|
// PowerMonitor is an EventEmitter.
|
2016-08-02 11:38:35 +00:00
|
|
|
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
2019-04-30 00:46:08 +00:00
|
|
|
|
|
|
|
const powerMonitor = createLazyInstance(createPowerMonitor, PowerMonitor, true)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-02-05 06:28:58 +00:00
|
|
|
// On Linux we need to call blockShutdown() to subscribe to shutdown event.
|
|
|
|
if (process.platform === 'linux') {
|
2019-04-30 00:46:08 +00:00
|
|
|
powerMonitor.on('newListener', (event:string) => {
|
2018-02-05 06:28:58 +00:00
|
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
|
|
|
powerMonitor.blockShutdown()
|
|
|
|
}
|
|
|
|
})
|
2017-12-07 13:10:40 +00:00
|
|
|
|
2019-04-30 00:46:08 +00:00
|
|
|
powerMonitor.on('removeListener', (event: string) => {
|
2018-02-05 06:28:58 +00:00
|
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
|
|
|
powerMonitor.unblockShutdown()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-12-07 13:10:40 +00:00
|
|
|
|
2019-02-27 20:54:01 +00:00
|
|
|
// TODO(nitsakh): Remove in 7.0
|
2019-04-30 00:46:08 +00:00
|
|
|
powerMonitor.querySystemIdleState = function (threshold: number, callback: Function) {
|
2019-02-27 20:54:01 +00:00
|
|
|
deprecate.warn('powerMonitor.querySystemIdleState', 'powerMonitor.getSystemIdleState')
|
2019-04-30 00:46:08 +00:00
|
|
|
if (typeof threshold !== 'number') throw new Error('Must pass threshold as a number')
|
|
|
|
if (typeof callback !== 'function') throw new Error('Must pass callback as a function argument')
|
2019-01-22 11:13:22 +00:00
|
|
|
|
2019-02-27 20:54:01 +00:00
|
|
|
const idleState = this.getSystemIdleState(threshold)
|
2019-01-22 11:13:22 +00:00
|
|
|
process.nextTick(() => callback(idleState))
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:54:01 +00:00
|
|
|
// TODO(nitsakh): Remove in 7.0
|
2019-04-30 00:46:08 +00:00
|
|
|
powerMonitor.querySystemIdleTime = function (callback: Function) {
|
2019-02-27 20:54:01 +00:00
|
|
|
deprecate.warn('powerMonitor.querySystemIdleTime', 'powerMonitor.getSystemIdleTime')
|
2019-04-30 00:46:08 +00:00
|
|
|
if (typeof callback !== 'function') throw new Error('Must pass function as an argument')
|
2019-01-22 11:13:22 +00:00
|
|
|
|
2019-02-27 20:54:01 +00:00
|
|
|
const idleTime = this.getSystemIdleTime()
|
2019-01-22 11:13:22 +00:00
|
|
|
process.nextTick(() => callback(idleTime))
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = powerMonitor
|