Signal-Desktop/js/modules/idle_detector.js

40 lines
740 B
JavaScript
Raw Normal View History

2018-03-21 19:17:32 +00:00
const desktopIdle = require('desktop-idle');
const EventEmitter = require('events');
const POLL_INTERVAL = 10; // seconds
const IDLE_THRESHOLD = POLL_INTERVAL;
2018-03-21 19:17:32 +00:00
class IdleDetector extends EventEmitter {
2018-03-21 19:17:32 +00:00
constructor() {
super();
this.intervalId = null;
}
start() {
this.stop();
this.intervalId = setInterval(() => {
const idleDurationInSeconds = desktopIdle.getIdleTime();
const isIdle = idleDurationInSeconds >= IDLE_THRESHOLD;
2018-03-21 19:17:32 +00:00
if (!isIdle) {
return;
}
this.emit('idle', { idleDurationInSeconds });
2018-03-21 19:17:32 +00:00
}, POLL_INTERVAL * 1000);
2018-03-21 19:17:32 +00:00
}
stop() {
if (!this.intervalId) {
return;
}
clearInterval(this.intervalId);
}
}
module.exports = {
IdleDetector,
2018-03-21 19:17:32 +00:00
};