feat: Add new powerMonitor synchronous API (#17144)
`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.
This commit is contained in:
parent
29ab74688d
commit
bfa07ec4be
7 changed files with 96 additions and 25 deletions
|
@ -84,7 +84,7 @@ void PowerMonitor::OnResume() {
|
||||||
Emit("resume");
|
Emit("resume");
|
||||||
}
|
}
|
||||||
|
|
||||||
ui::IdleState PowerMonitor::QuerySystemIdleState(v8::Isolate* isolate,
|
ui::IdleState PowerMonitor::GetSystemIdleState(v8::Isolate* isolate,
|
||||||
int idle_threshold) {
|
int idle_threshold) {
|
||||||
if (idle_threshold > 0) {
|
if (idle_threshold > 0) {
|
||||||
return ui::CalculateIdleState(idle_threshold);
|
return ui::CalculateIdleState(idle_threshold);
|
||||||
|
@ -95,7 +95,7 @@ ui::IdleState PowerMonitor::QuerySystemIdleState(v8::Isolate* isolate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int PowerMonitor::QuerySystemIdleTime() {
|
int PowerMonitor::GetSystemIdleTime() {
|
||||||
return ui::CalculateIdleTime();
|
return ui::CalculateIdleTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +122,8 @@ void PowerMonitor::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
|
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
|
||||||
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown)
|
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown)
|
||||||
#endif
|
#endif
|
||||||
.SetMethod("_querySystemIdleState", &PowerMonitor::QuerySystemIdleState)
|
.SetMethod("getSystemIdleState", &PowerMonitor::GetSystemIdleState)
|
||||||
.SetMethod("_querySystemIdleTime", &PowerMonitor::QuerySystemIdleTime);
|
.SetMethod("getSystemIdleTime", &PowerMonitor::GetSystemIdleTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -46,8 +46,8 @@ class PowerMonitor : public mate::TrackableObject<PowerMonitor>,
|
||||||
void OnResume() override;
|
void OnResume() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ui::IdleState QuerySystemIdleState(v8::Isolate* isolate, int idle_threshold);
|
ui::IdleState GetSystemIdleState(v8::Isolate* isolate, int idle_threshold);
|
||||||
int QuerySystemIdleTime();
|
int GetSystemIdleTime();
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
// Static callback invoked when a message comes in to our messaging window.
|
// Static callback invoked when a message comes in to our messaging window.
|
||||||
|
|
|
@ -63,6 +63,24 @@ require('path')
|
||||||
require('electron').remote.require('path')
|
require('electron').remote.require('path')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `powerMonitor.querySystemIdleState`
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Deprecated
|
||||||
|
powerMonitor.querySystemIdleState(threshold, callback)
|
||||||
|
// Replace with synchronous API
|
||||||
|
const idleState = getSystemIdleState(threshold)
|
||||||
|
```
|
||||||
|
|
||||||
|
## `powerMonitor.querySystemIdleTime`
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Deprecated
|
||||||
|
powerMonitor.querySystemIdleTime(callback)
|
||||||
|
// Replace with synchronous API
|
||||||
|
const idleTime = getSystemIdleTime()
|
||||||
|
```
|
||||||
|
|
||||||
# Planned Breaking API Changes (5.0)
|
# Planned Breaking API Changes (5.0)
|
||||||
|
|
||||||
## `new BrowserWindow({ webPreferences })`
|
## `new BrowserWindow({ webPreferences })`
|
||||||
|
|
|
@ -59,7 +59,7 @@ Emitted as soon as the systems screen is unlocked.
|
||||||
|
|
||||||
The `powerMonitor` module has the following methods:
|
The `powerMonitor` module has the following methods:
|
||||||
|
|
||||||
#### `powerMonitor.querySystemIdleState(idleThreshold, callback)`
|
#### `powerMonitor.querySystemIdleState(idleThreshold, callback)` _(Deprecated)_
|
||||||
|
|
||||||
* `idleThreshold` Integer
|
* `idleThreshold` Integer
|
||||||
* `callback` Function
|
* `callback` Function
|
||||||
|
@ -70,9 +70,25 @@ before considered idle. `callback` will be called synchronously on some systems
|
||||||
and with an `idleState` argument that describes the system's state. `locked` is
|
and with an `idleState` argument that describes the system's state. `locked` is
|
||||||
available on supported systems only.
|
available on supported systems only.
|
||||||
|
|
||||||
#### `powerMonitor.querySystemIdleTime(callback)`
|
#### `powerMonitor.querySystemIdleTime(callback)` _(Deprecated)_
|
||||||
|
|
||||||
* `callback` Function
|
* `callback` Function
|
||||||
* `idleTime` Integer - Idle time in seconds
|
* `idleTime` Integer - Idle time in seconds
|
||||||
|
|
||||||
Calculate system idle time in seconds.
|
Calculate system idle time in seconds.
|
||||||
|
|
||||||
|
#### `powerMonitor.getSystemIdleState(idleThreshold)`
|
||||||
|
|
||||||
|
* `idleThreshold` Integer
|
||||||
|
|
||||||
|
Returns `String` - The system's current state. Can be `active`, `idle`, `locked` or `unknown`.
|
||||||
|
|
||||||
|
Calculate the system idle state. `idleThreshold` is the amount of time (in seconds)
|
||||||
|
before considered idle. `locked` is available on supported systems only.
|
||||||
|
|
||||||
|
#### `powerMonitor.getSystemIdleTime()`
|
||||||
|
|
||||||
|
Returns `Integer` - Idle time in seconds
|
||||||
|
|
||||||
|
Calculate system idle time in seconds.
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const { EventEmitter } = require('events')
|
||||||
const { powerMonitor, PowerMonitor } = process.atomBinding('power_monitor')
|
const { powerMonitor, PowerMonitor } = process.atomBinding('power_monitor')
|
||||||
|
const { deprecate } = require('electron')
|
||||||
|
|
||||||
// PowerMonitor is an EventEmitter.
|
// PowerMonitor is an EventEmitter.
|
||||||
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
||||||
|
@ -22,8 +23,9 @@ if (process.platform === 'linux') {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
|
// TODO(nitsakh): Remove in 7.0
|
||||||
powerMonitor.querySystemIdleState = function (threshold, callback) {
|
powerMonitor.querySystemIdleState = function (threshold, callback) {
|
||||||
|
deprecate.warn('powerMonitor.querySystemIdleState', 'powerMonitor.getSystemIdleState')
|
||||||
if (typeof threshold !== 'number') {
|
if (typeof threshold !== 'number') {
|
||||||
throw new Error('Must pass threshold as a number')
|
throw new Error('Must pass threshold as a number')
|
||||||
}
|
}
|
||||||
|
@ -32,18 +34,19 @@ powerMonitor.querySystemIdleState = function (threshold, callback) {
|
||||||
throw new Error('Must pass callback as a function argument')
|
throw new Error('Must pass callback as a function argument')
|
||||||
}
|
}
|
||||||
|
|
||||||
const idleState = this._querySystemIdleState(threshold)
|
const idleState = this.getSystemIdleState(threshold)
|
||||||
|
|
||||||
process.nextTick(() => callback(idleState))
|
process.nextTick(() => callback(idleState))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(deepak1556): Deprecate async api in favor of sync version in 5.0
|
// TODO(nitsakh): Remove in 7.0
|
||||||
powerMonitor.querySystemIdleTime = function (callback) {
|
powerMonitor.querySystemIdleTime = function (callback) {
|
||||||
|
deprecate.warn('powerMonitor.querySystemIdleTime', 'powerMonitor.getSystemIdleTime')
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new Error('Must pass function as an argument')
|
throw new Error('Must pass function as an argument')
|
||||||
}
|
}
|
||||||
|
|
||||||
const idleTime = this._querySystemIdleTime()
|
const idleTime = this.getSystemIdleTime()
|
||||||
|
|
||||||
process.nextTick(() => callback(idleTime))
|
process.nextTick(() => callback(idleTime))
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,6 +128,7 @@ describe('powerMonitor', () => {
|
||||||
powerMonitor = require('electron').remote.powerMonitor
|
powerMonitor = require('electron').remote.powerMonitor
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO(nitsakh): Remove in 7.0
|
||||||
describe('powerMonitor.querySystemIdleState', () => {
|
describe('powerMonitor.querySystemIdleState', () => {
|
||||||
it('notify current system idle state', done => {
|
it('notify current system idle state', done => {
|
||||||
// this function is not mocked out, so we can test the result's
|
// this function is not mocked out, so we can test the result's
|
||||||
|
@ -155,6 +156,7 @@ describe('powerMonitor', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO(nitsakh): Remove in 7.0
|
||||||
describe('powerMonitor.querySystemIdleTime', () => {
|
describe('powerMonitor.querySystemIdleTime', () => {
|
||||||
it('notify current system idle time', done => {
|
it('notify current system idle time', done => {
|
||||||
powerMonitor.querySystemIdleTime(idleTime => {
|
powerMonitor.querySystemIdleTime(idleTime => {
|
||||||
|
@ -163,5 +165,37 @@ describe('powerMonitor', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('powerMonitor.getSystemIdleState', () => {
|
||||||
|
it('gets current system idle state', () => {
|
||||||
|
// this function is not mocked out, so we can test the result's
|
||||||
|
// form and type but not its value.
|
||||||
|
const idleState = powerMonitor.getSystemIdleState(1)
|
||||||
|
expect(idleState).to.be.a('string')
|
||||||
|
const validIdleStates = [ 'active', 'idle', 'locked', 'unknown' ]
|
||||||
|
expect(validIdleStates).to.include(idleState)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not accept non positive integer threshold', () => {
|
||||||
|
expect(() => {
|
||||||
|
powerMonitor.getSystemIdleState(-1)
|
||||||
|
}).to.throw(/must be greater than 0/)
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
powerMonitor.getSystemIdleState(NaN)
|
||||||
|
}).to.throw(/conversion failure/)
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
powerMonitor.getSystemIdleState('a')
|
||||||
|
}).to.throw(/conversion failure/)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('powerMonitor.getSystemIdleTime', () => {
|
||||||
|
it('notify current system idle time', () => {
|
||||||
|
const idleTime = powerMonitor.getSystemIdleTime()
|
||||||
|
expect(idleTime).to.be.at.least(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
24
spec/package-lock.json
generated
24
spec/package-lock.json
generated
|
@ -75,7 +75,7 @@
|
||||||
},
|
},
|
||||||
"bl": {
|
"bl": {
|
||||||
"version": "1.2.2",
|
"version": "1.2.2",
|
||||||
"resolved": "http://registry.npmjs.org/bl/-/bl-1.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz",
|
||||||
"integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==",
|
"integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
@ -231,7 +231,7 @@
|
||||||
},
|
},
|
||||||
"commander": {
|
"commander": {
|
||||||
"version": "2.15.1",
|
"version": "2.15.1",
|
||||||
"resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
|
"resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
|
||||||
"integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
|
"integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -363,7 +363,7 @@
|
||||||
},
|
},
|
||||||
"duplexer": {
|
"duplexer": {
|
||||||
"version": "0.1.1",
|
"version": "0.1.1",
|
||||||
"resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz",
|
||||||
"integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=",
|
"integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -781,7 +781,7 @@
|
||||||
},
|
},
|
||||||
"mkdirp": {
|
"mkdirp": {
|
||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
|
||||||
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
|
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
|
||||||
"requires": {
|
"requires": {
|
||||||
"minimist": "0.0.8"
|
"minimist": "0.0.8"
|
||||||
|
@ -789,7 +789,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
||||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
|
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -975,7 +975,7 @@
|
||||||
},
|
},
|
||||||
"os-homedir": {
|
"os-homedir": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "http://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
|
||||||
"integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=",
|
"integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=",
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
|
@ -1040,7 +1040,7 @@
|
||||||
},
|
},
|
||||||
"path-is-absolute": {
|
"path-is-absolute": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
||||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
|
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -1058,7 +1058,7 @@
|
||||||
},
|
},
|
||||||
"pause-stream": {
|
"pause-stream": {
|
||||||
"version": "0.0.11",
|
"version": "0.0.11",
|
||||||
"resolved": "http://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz",
|
"resolved": "https://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz",
|
||||||
"integrity": "sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=",
|
"integrity": "sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
@ -1357,7 +1357,7 @@
|
||||||
},
|
},
|
||||||
"string_decoder": {
|
"string_decoder": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
|
||||||
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
|
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"safe-buffer": "~5.1.0"
|
"safe-buffer": "~5.1.0"
|
||||||
|
@ -1373,7 +1373,7 @@
|
||||||
},
|
},
|
||||||
"strip-eof": {
|
"strip-eof": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
|
||||||
"integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=",
|
"integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -1442,7 +1442,7 @@
|
||||||
},
|
},
|
||||||
"through": {
|
"through": {
|
||||||
"version": "2.3.8",
|
"version": "2.3.8",
|
||||||
"resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||||
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
|
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -1537,7 +1537,7 @@
|
||||||
},
|
},
|
||||||
"wrap-ansi": {
|
"wrap-ansi": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz",
|
||||||
"integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=",
|
"integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
|
Loading…
Reference in a new issue