power-monitor.js 1.5 KB

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