Rename IdleListener
to IdleDetector
This commit is contained in:
parent
92ace67846
commit
d13668544d
3 changed files with 12 additions and 12 deletions
39
js/modules/idle_detector.js
Normal file
39
js/modules/idle_detector.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const desktopIdle = require('desktop-idle');
|
||||
const EventEmitter = require('events');
|
||||
|
||||
|
||||
const POLL_INTERVAL = 10; // seconds
|
||||
const IDLE_THRESHOLD = POLL_INTERVAL;
|
||||
|
||||
class IdleDetector extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.intervalId = null;
|
||||
}
|
||||
|
||||
start() {
|
||||
this.stop();
|
||||
this.intervalId = setInterval(() => {
|
||||
const idleDurationInSeconds = desktopIdle.getIdleTime();
|
||||
const isIdle = idleDurationInSeconds >= IDLE_THRESHOLD;
|
||||
if (!isIdle) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('idle', { idleDurationInSeconds });
|
||||
|
||||
}, POLL_INTERVAL * 1000);
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.intervalId) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearInterval(this.intervalId);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
IdleDetector,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue