Skip to content

Commit

Permalink
Idle detector: Fix bug preventing it from ever turning off (#2487)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Jul 3, 2018
1 parent bd13939 commit 125c3fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
let isMigrationWithIndexComplete = false;
let isMigrationWithoutIndexComplete = false;
idleDetector.on('idle', async () => {
console.log('Idle processing started');
const NUM_MESSAGES_PER_BATCH = 1;

if (!isMigrationWithIndexComplete) {
Expand Down Expand Up @@ -108,6 +109,7 @@
const areAllMigrationsComplete =
isMigrationWithIndexComplete && isMigrationWithoutIndexComplete;
if (areAllMigrationsComplete) {
console.log('All migrations are complete. Stopping idle detector.');
idleDetector.stop();
}
});
Expand Down
12 changes: 9 additions & 3 deletions js/modules/idle_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ class IdleDetector extends EventEmitter {
}

stop() {
if (!this.handle) {
return;
}

console.log('Stop idle detector');
this._clearScheduledCallbacks();
}

_clearScheduledCallbacks() {
if (this.handle) {
cancelIdleCallback(this.handle);
this.handle = null;
}

if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}

Expand All @@ -38,13 +44,13 @@ class IdleDetector extends EventEmitter {
const { didTimeout } = deadline;
const timeRemaining = deadline.timeRemaining();
const isIdle = timeRemaining >= IDLE_THRESHOLD_MS;
if (isIdle || didTimeout) {
this.emit('idle', { timestamp: Date.now(), didTimeout, timeRemaining });
}
this.timeoutId = setTimeout(
() => this._scheduleNextCallback(),
POLL_INTERVAL_MS
);
if (isIdle || didTimeout) {
this.emit('idle', { timestamp: Date.now(), didTimeout, timeRemaining });
}
});
}
}
Expand Down

0 comments on commit 125c3fa

Please sign in to comment.