Skip to content

Commit

Permalink
Don't purge the cache on *any* alarm (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Sep 11, 2020
1 parent 7b3184e commit 1354876
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,8 @@ async function deleteWithLogic(logic?: (x: CacheItem<Value>) => boolean): Promis
}
}

let lastRun = 0; // Homemade debouncing due to `chrome.alarms` potentially queueing this function
async function deleteExpired(): Promise<void> {
if (lastRun < Date.now() - 1000) {
lastRun = Date.now();
await deleteWithLogic(cachedItem => Date.now() > cachedItem.maxAge);
}
await deleteWithLogic(cachedItem => Date.now() > cachedItem.maxAge);
}

async function clear(): Promise<void> {
Expand Down Expand Up @@ -180,7 +176,14 @@ function init(): void {
delayInMinutes: 1,
periodInMinutes: 60 * 24
});
chrome.alarms.onAlarm.addListener(deleteExpired);

let lastRun = 0; // Homemade debouncing due to `chrome.alarms` potentially queueing this function
chrome.alarms.onAlarm.addListener(alarm => {
if (alarm.name === 'webext-storage-cache' && lastRun < Date.now() - 1000) {
lastRun = Date.now();
deleteExpired();
}
});
} else {
setTimeout(deleteExpired, 60000); // Purge cache on launch, but wait a bit
setInterval(deleteExpired, 1000 * 3600 * 24);
Expand Down

0 comments on commit 1354876

Please sign in to comment.