power-monitor.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict'
  2. const { EventEmitter } = require('events')
  3. const { powerMonitor, PowerMonitor } = process.electronBinding('power_monitor')
  4. const { deprecate } = require('electron')
  5. // PowerMonitor is an EventEmitter.
  6. Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
  7. EventEmitter.call(powerMonitor)
  8. // On Linux we need to call blockShutdown() to subscribe to shutdown event.
  9. if (process.platform === 'linux') {
  10. powerMonitor.on('newListener', (event) => {
  11. if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
  12. powerMonitor.blockShutdown()
  13. }
  14. })
  15. powerMonitor.on('removeListener', (event) => {
  16. if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
  17. powerMonitor.unblockShutdown()
  18. }
  19. })
  20. }
  21. // TODO(nitsakh): Remove in 7.0
  22. powerMonitor.querySystemIdleState = function (threshold, callback) {
  23. deprecate.warn('powerMonitor.querySystemIdleState', 'powerMonitor.getSystemIdleState')
  24. if (typeof threshold !== 'number') {
  25. throw new Error('Must pass threshold as a number')
  26. }
  27. if (typeof callback !== 'function') {
  28. throw new Error('Must pass callback as a function argument')
  29. }
  30. const idleState = this.getSystemIdleState(threshold)
  31. process.nextTick(() => callback(idleState))
  32. }
  33. // TODO(nitsakh): Remove in 7.0
  34. powerMonitor.querySystemIdleTime = function (callback) {
  35. deprecate.warn('powerMonitor.querySystemIdleTime', 'powerMonitor.getSystemIdleTime')
  36. if (typeof callback !== 'function') {
  37. throw new Error('Must pass function as an argument')
  38. }
  39. const idleTime = this.getSystemIdleTime()
  40. process.nextTick(() => callback(idleTime))
  41. }
  42. module.exports = powerMonitor