run a cache cleanup next time if popup is closed too soon

This commit is contained in:
tophf 2018-07-05 15:45:31 +03:00
parent 0d7b8152e8
commit 626030c42c

View File

@ -44,7 +44,8 @@ window.addEventListener('showStyles:done', function _() {
const CACHE_SIZE = 1e6; const CACHE_SIZE = 1e6;
const CACHE_PREFIX = 'usoSearchCache/'; const CACHE_PREFIX = 'usoSearchCache/';
const CACHE_DURATION = 24 * 3600e3; const CACHE_DURATION = 24 * 3600e3;
const CACHE_CLEANUP_THROTTLE = 60e3; const CACHE_CLEANUP_THROTTLE = 10e3;
const CACHE_CLEANUP_NEEDED = CACHE_PREFIX + 'clean?';
const CACHE_EXCEPT_PROPS = ['css', 'discussions', 'additional_info']; const CACHE_EXCEPT_PROPS = ['css', 'discussions', 'additional_info'];
let searchTotalPages; let searchTotalPages;
@ -148,6 +149,9 @@ window.addEventListener('showStyles:done', function _() {
renderActionButtons($('#' + RESULT_ID_PREFIX + usoId)); renderActionButtons($('#' + RESULT_ID_PREFIX + usoId));
} }
}); });
chromeLocal.getValue(CACHE_CLEANUP_NEEDED).then(value =>
value && debounce(cleanupCache, CACHE_CLEANUP_THROTTLE));
} }
//endregion //endregion
@ -737,6 +741,7 @@ window.addEventListener('showStyles:done', function _() {
setTimeout(writeCache, 100, data, true); setTimeout(writeCache, 100, data, true);
return data; return data;
} else { } else {
chromeLocal.setValue(CACHE_CLEANUP_NEEDED, true);
debounce(cleanupCache, CACHE_CLEANUP_THROTTLE); debounce(cleanupCache, CACHE_CLEANUP_THROTTLE);
return chromeLocal.loadLZStringScript().then(() => return chromeLocal.loadLZStringScript().then(() =>
chromeLocal.setValue(CACHE_PREFIX + data.id, { chromeLocal.setValue(CACHE_PREFIX + data.id, {
@ -751,15 +756,16 @@ window.addEventListener('showStyles:done', function _() {
} }
function cleanupCache() { function cleanupCache() {
if (!chrome.storage.local.getBytesInUse) { chromeLocal.remove(CACHE_CLEANUP_NEEDED);
chrome.storage.local.get(null, cleanupCacheInternal); if (chrome.storage.local.getBytesInUse) {
} else {
chrome.storage.local.getBytesInUse(null, size => { chrome.storage.local.getBytesInUse(null, size => {
if (size > CACHE_SIZE) { if (size > CACHE_SIZE) {
chrome.storage.local.get(null, cleanupCacheInternal); chrome.storage.local.get(null, cleanupCacheInternal);
} }
ignoreChromeError(); ignoreChromeError();
}); });
} else {
chrome.storage.local.get(null, cleanupCacheInternal);
} }
} }