SDA-1549 - Prevent multiple intervals for activity detection (#792)

This commit is contained in:
Kiran Niranjan 2019-10-23 18:39:10 +05:30 committed by Vishwas Shashidhar
parent 627ea097e1
commit 85e61b8c60

View File

@ -9,6 +9,7 @@ class ActivityDetection {
private idleThreshold: number; private idleThreshold: number;
private window: Electron.WebContents | null; private window: Electron.WebContents | null;
private timer: Timer | undefined; private timer: Timer | undefined;
private queryInterval: NodeJS.Timer | undefined;
constructor() { constructor() {
this.idleThreshold = 60 * 60 * 1000; this.idleThreshold = 60 * 60 * 1000;
@ -24,6 +25,9 @@ class ActivityDetection {
public setWindowAndThreshold(window: Electron.WebContents, idleThreshold: number): void { public setWindowAndThreshold(window: Electron.WebContents, idleThreshold: number): void {
this.window = window; this.window = window;
this.idleThreshold = idleThreshold; this.idleThreshold = idleThreshold;
if (this.queryInterval) {
clearInterval(this.queryInterval);
}
this.startActivityMonitor(); this.startActivityMonitor();
logger.info(`activity-detection: Initialized activity detection with an idleThreshold of ${idleThreshold}`); logger.info(`activity-detection: Initialized activity detection with an idleThreshold of ${idleThreshold}`);
} }
@ -34,7 +38,7 @@ class ActivityDetection {
private startActivityMonitor(): void { private startActivityMonitor(): void {
if (app.isReady()) { if (app.isReady()) {
logger.info(`activity-detection: Starting activity monitor`); logger.info(`activity-detection: Starting activity monitor`);
setInterval(() => (electron.powerMonitor as any).querySystemIdleTime(this.activity.bind(this)), this.idleThreshold); this.queryInterval = setInterval(() => (electron.powerMonitor as any).querySystemIdleTime(this.activity.bind(this)), this.idleThreshold);
} }
} }