Implement basic `IdleListener`

This commit is contained in:
Daniel Gasienica 2018-03-21 15:17:32 -04:00
parent ca2afdc202
commit 3140e4d66d
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
const desktopIdle = require('desktop-idle');
const EventEmitter = require('events');
const POLL_INTERVAL_MS = 10 * 1000;
const IDLE_THRESHOLD_MS = POLL_INTERVAL_MS;
class IdleListener extends EventEmitter {
constructor() {
super();
this.intervalId = null;
}
start() {
this.stop();
this.intervalId = setInterval(() => {
const idleDuration = desktopIdle.getIdleTime();
const isIdle = idleDuration >= (IDLE_THRESHOLD_MS / 1000);
if (!isIdle) {
return;
}
this.emit('idle', { idleDuration });
}, POLL_INTERVAL_MS);
}
stop() {
if (!this.intervalId) {
return;
}
clearInterval(this.intervalId);
}
}
module.exports = {
IdleListener,
};