bfa07ec4be
`powerMonitor.querySystemIdleState` and `powerMonitor.querySystemIdleTime` had async backing APIs in chromium (https://chromium-review.googlesource.com/c/chromium/src/+/1379183). However, that has changed in ch73. So, this PR deprecates the old async APIs and adds new sync APIs.
54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
'use strict'
|
|
|
|
const { EventEmitter } = require('events')
|
|
const { powerMonitor, PowerMonitor } = process.atomBinding('power_monitor')
|
|
const { deprecate } = require('electron')
|
|
|
|
// PowerMonitor is an EventEmitter.
|
|
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
|
EventEmitter.call(powerMonitor)
|
|
|
|
// On Linux we need to call blockShutdown() to subscribe to shutdown event.
|
|
if (process.platform === 'linux') {
|
|
powerMonitor.on('newListener', (event) => {
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
|
powerMonitor.blockShutdown()
|
|
}
|
|
})
|
|
|
|
powerMonitor.on('removeListener', (event) => {
|
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
|
powerMonitor.unblockShutdown()
|
|
}
|
|
})
|
|
}
|
|
|
|
// TODO(nitsakh): Remove in 7.0
|
|
powerMonitor.querySystemIdleState = function (threshold, callback) {
|
|
deprecate.warn('powerMonitor.querySystemIdleState', 'powerMonitor.getSystemIdleState')
|
|
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')
|
|
}
|
|
|
|
const idleState = this.getSystemIdleState(threshold)
|
|
|
|
process.nextTick(() => callback(idleState))
|
|
}
|
|
|
|
// TODO(nitsakh): Remove in 7.0
|
|
powerMonitor.querySystemIdleTime = function (callback) {
|
|
deprecate.warn('powerMonitor.querySystemIdleTime', 'powerMonitor.getSystemIdleTime')
|
|
if (typeof callback !== 'function') {
|
|
throw new Error('Must pass function as an argument')
|
|
}
|
|
|
|
const idleTime = this.getSystemIdleTime()
|
|
|
|
process.nextTick(() => callback(idleTime))
|
|
}
|
|
|
|
module.exports = powerMonitor
|