asarfuckery/electronasar/development/browser/api/power-monitor.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-17 18:22:05 +00:00
'use strict'
const { EventEmitter } = require('events')
const { powerMonitor, PowerMonitor } = process.atomBinding('power_monitor')
// 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()
}
})
}
2019-04-04 23:07:03 +00:00
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
powerMonitor.querySystemIdleState = function (threshold, callback) {
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._querySystemIdleState(threshold)
process.nextTick(() => callback(idleState))
}
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
powerMonitor.querySystemIdleTime = function (callback) {
if (typeof callback !== 'function') {
throw new Error('Must pass function as an argument')
}
const idleTime = this._querySystemIdleTime()
process.nextTick(() => callback(idleTime))
}
2019-01-17 18:22:05 +00:00
module.exports = powerMonitor