You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
740 B
JavaScript
40 lines
740 B
JavaScript
7 years ago
|
const desktopIdle = require('desktop-idle');
|
||
|
const EventEmitter = require('events');
|
||
|
|
||
|
|
||
7 years ago
|
const POLL_INTERVAL = 10; // seconds
|
||
|
const IDLE_THRESHOLD = POLL_INTERVAL;
|
||
7 years ago
|
|
||
7 years ago
|
class IdleDetector extends EventEmitter {
|
||
7 years ago
|
constructor() {
|
||
|
super();
|
||
|
this.intervalId = null;
|
||
|
}
|
||
|
|
||
|
start() {
|
||
|
this.stop();
|
||
|
this.intervalId = setInterval(() => {
|
||
7 years ago
|
const idleDurationInSeconds = desktopIdle.getIdleTime();
|
||
|
const isIdle = idleDurationInSeconds >= IDLE_THRESHOLD;
|
||
7 years ago
|
if (!isIdle) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
this.emit('idle', { idleDurationInSeconds });
|
||
7 years ago
|
|
||
7 years ago
|
}, POLL_INTERVAL * 1000);
|
||
7 years ago
|
}
|
||
|
|
||
|
stop() {
|
||
|
if (!this.intervalId) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
clearInterval(this.intervalId);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
7 years ago
|
IdleDetector,
|
||
7 years ago
|
};
|