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

43 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-10-01 13:49:56 +00:00
'use strict';
const { EventEmitter } = require('events');
const { powerMonitor, PowerMonitor } = process.electronBinding('power_monitor');
const { deprecate } = require('electron');
2019-01-17 18:22:05 +00:00
// PowerMonitor is an EventEmitter.
2019-10-01 13:49:56 +00:00
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype);
EventEmitter.call(powerMonitor);
2019-01-17 18:22:05 +00:00
// On Linux we need to call blockShutdown() to subscribe to shutdown event.
if (process.platform === 'linux') {
2019-10-01 13:49:56 +00:00
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');
2019-01-17 18:22:05 +00:00
}
2019-10-01 13:49:56 +00:00
if (typeof callback !== 'function') {
throw new Error('Must pass callback as a function argument');
2019-01-17 18:22:05 +00:00
}
2019-10-01 13:49:56 +00:00
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;