Skip to content

Commit

Permalink
store globalAPICache locally
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish Sarangi committed Aug 1, 2024
1 parent 83701df commit 18e54e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
70 changes: 26 additions & 44 deletions webextension/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ importScripts('utils.js')
//let gStatusCode = 0
// let gToolbarStates = {}
// let waybackCountCache = {}
// let globalAPICache = new Map()
let globalAPICache = new Map()
const API_CACHE_SIZE = 5
const API_LOADING = 'LOADING'
const API_TIMEOUT = 10000
Expand Down Expand Up @@ -330,50 +330,32 @@ async function fetchAPI(url, onSuccess, onFail, postData = null) {
* @param postData {object}: uses POST if present.
* @return Promise if calls API, json data if in cache, null if loading in progress.
*/
async function fetchCachedAPI(url, onSuccess, onFail, postData = null) {
chrome.storage.local.get(['globalAPICache'], async function(result) {
let globalAPICache = result.globalAPICache ? new Map(Object.entries(result.globalAPICache)) : new Map();
let data = globalAPICache.get(url);

if (data === API_LOADING) {
setTimeout(() => {
fetchCachedAPI(url, onSuccess, onFail, postData)
}, API_RETRY);
return null;
} else if (data !== undefined) {
onSuccess(data);
return data;
} else {
// if cache full, remove first object which is the oldest from the cache
if (globalAPICache.size >= API_CACHE_SIZE) {
globalAPICache.delete(globalAPICache.keys().next().value);
}
globalAPICache.set(url, API_LOADING);

// Convert Map back to object before storing it in chrome.storage
let globalAPICacheObject = Object.fromEntries(globalAPICache);

chrome.storage.local.set({ globalAPICache: globalAPICacheObject }, function() {
fetchAPI(url, (json) => {
globalAPICache.set(url, json);

// Convert Map back to object before storing it in chrome.storage
let globalAPICacheObject = Object.fromEntries(globalAPICache);
chrome.storage.local.set({ globalAPICache: globalAPICacheObject });

onSuccess(json);
}, (error) => {
globalAPICache.delete(url);

// Convert Map back to object before storing it in chrome.storage
let globalAPICacheObject = Object.fromEntries(globalAPICache);
chrome.storage.local.set({ globalAPICache: globalAPICacheObject });

onFail(error);
}, postData);
});
function fetchCachedAPI(url, onSuccess, onFail, postData = null) {
if (typeof globalAPICache === 'undefined') { globalAPICache = new Map() }
let data = globalAPICache.get(url)
if (data === API_LOADING) {
// re-call after delay if previous fetch hadn't returned yet
setTimeout(() => {
fetchCachedAPI(url, onSuccess, onFail, postData)
}, API_RETRY)
return null
} else if (data !== undefined) {
onSuccess(data)
return data
} else {
// if cache full, remove first object which is the oldest from the cache
if (globalAPICache.size >= API_CACHE_SIZE) {
globalAPICache.delete(globalAPICache.keys().next().value)
}
});
globalAPICache.set(url, API_LOADING)
return fetchAPI(url, (json) => {
globalAPICache.set(url, json)
onSuccess(json)
}, (error) => {
globalAPICache.delete(url)
onFail(error)
}, postData)
}
}


Expand Down
1 change: 0 additions & 1 deletion webextension/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,6 @@ function initDefaultOptions () {
gStatusCode: 0,
waybackCountCache: {},
gToolbarStates: {},
globalAPICache: {},
/* Features */
private_mode_setting: true,
not_found_setting: false,
Expand Down

0 comments on commit 18e54e8

Please sign in to comment.