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.
session-desktop/js/modules/idle_detector.js

40 lines
740 B
JavaScript

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,
};