Skip to content

Commit

Permalink
Label first lost event alarm correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorallen committed May 15, 2024
1 parent b113950 commit 70ae5a3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function scheduleCheckToClose() {
}

async function checkToClose() {
console.debug("[checkToClose] Checking…", new Date().toISOString());
const startTime = Date.now();
try {
const storageSyncPersist = await getStorageSyncPersist();
Expand Down Expand Up @@ -283,8 +282,8 @@ async function startup() {

startup();

// Keep the [service worker (Chrome) / background script (Firefox)] alive so the popup can always
// talk to it to access the Store.
// Keep the [service worker (Chrome) / background script (Firefox)] alive so background can check
// for tabs to close frequently.
// Self-contained workaround for https://crbug.com/1316588 (Apache License)
// Source: https://bugs.chromium.org/p/chromium/issues/detail?id=1316588#c99
let lastAlarm = 0;
Expand All @@ -295,14 +294,18 @@ let lastAlarm = 0;
await new Promise((resolve) => setTimeout(resolve, 65000));
const now = Date.now();
const age = now - lastAlarm;
console.debug(`lostEventsWatchdog: last alarm ${age / 1000}s ago`);
console.debug(
lastAlarm === 0
? `[lostEventsWatchdog]: first alarm`
: `[lostEventsWatchdog]: last alarm ${age / 1000}s ago`,
);
if (age < 95000) {
quietCount = 0; // alarm still works.
} else if (++quietCount >= 3) {
console.warn("lostEventsWatchdog: reloading!");
console.warn("[lostEventsWatchdog]: reloading!");
return chrome.runtime.reload();
} else {
chrome.alarms.create(`lostEventsWatchdog/${now}`, { delayInMinutes: 1 });
chrome.alarms.create(`lostEventsWatchdog/${now}`, { delayInMinutes: 0.5 });
}
}
})();
Expand Down

0 comments on commit 70ae5a3

Please sign in to comment.