From 85e61b8c601235b1e1bfed2dca38f74842961498 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Wed, 23 Oct 2019 18:39:10 +0530 Subject: [PATCH] SDA-1549 - Prevent multiple intervals for activity detection (#792) --- src/app/activity-detection.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/activity-detection.ts b/src/app/activity-detection.ts index f0039102..fbd3a70d 100644 --- a/src/app/activity-detection.ts +++ b/src/app/activity-detection.ts @@ -9,6 +9,7 @@ class ActivityDetection { private idleThreshold: number; private window: Electron.WebContents | null; private timer: Timer | undefined; + private queryInterval: NodeJS.Timer | undefined; constructor() { this.idleThreshold = 60 * 60 * 1000; @@ -24,6 +25,9 @@ class ActivityDetection { public setWindowAndThreshold(window: Electron.WebContents, idleThreshold: number): void { this.window = window; this.idleThreshold = idleThreshold; + if (this.queryInterval) { + clearInterval(this.queryInterval); + } this.startActivityMonitor(); logger.info(`activity-detection: Initialized activity detection with an idleThreshold of ${idleThreshold}`); } @@ -34,7 +38,7 @@ class ActivityDetection { private startActivityMonitor(): void { if (app.isReady()) { 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); } }