feat(scripts): add validity check to cache, improve documentation and improve cache related methods

This commit is contained in:
wanhose 2021-06-23 13:25:43 +02:00
parent 456783543c
commit 61cb55fa39

View File

@ -9,11 +9,24 @@ const cacheInitialState = {
matches: [], matches: [],
}; };
/**
* @function isValid
* @description Check cache validity
*
* @param {object} [cache]
*/
const isValid = (cache) =>
typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string");
/** /**
* @function disableIcon * @function disableIcon
* @description Disables icon * @description Disables icon
* *
* @param {string} [tabId] * @param {string} [tabId]
* @returns {boolean}
*/ */
const disableIcon = (tabId) => { const disableIcon = (tabId) => {
@ -70,16 +83,20 @@ const enablePopup = (tabId) => {
* @function getCache * @function getCache
* @description Retrieves cache state * @description Retrieves cache state
* *
* @param {string} [hostname]
* @param {void} [responseCallback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state
*/ */
const getCache = async (hostname, responseCallback) => { const getCache = async (hostname, responseCallback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
const cache = store[hostname]; try {
const cache = store[hostname];
if (!isValid(cache)) throw new Error();
if (cache) {
responseCallback(cache); responseCallback(cache);
} else { } catch {
chrome.storage.local.set({ [hostname]: cacheInitialState }); chrome.storage.local.set({ [hostname]: cacheInitialState });
responseCallback(cacheInitialState); responseCallback(cacheInitialState);
} }
@ -88,19 +105,18 @@ const getCache = async (hostname, responseCallback) => {
/** /**
* @async * @async
* @function updateCache * @function getTab
* @description Update cache state * @description Retrieves current tab information
*
* @param {void} [responseCallback]
* @returns {Promise<{ id: string, location: string }>} Current tab information
*/ */
const updateCache = async (hostname, selector) => { const getTab = (responseCallback) => {
chrome.storage.local.get(null, (cache) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const current = cache[hostname]; responseCallback({
id: tabs[0].id,
chrome.storage.local.set({ hostname: new URL(tabs[0].url).hostname,
[hostname]: {
...current,
matches: [...new Set([...current.matches, selector])],
},
}); });
}); });
}; };
@ -110,6 +126,7 @@ const updateCache = async (hostname, selector) => {
* @function getList * @function getList
* @description Retrieves selectors list * @description Retrieves selectors list
* *
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
@ -122,12 +139,40 @@ const getList = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ matches: data.split("\n") }); responseCallback({ selectors: data.split("\n") });
} catch { } catch {
responseCallback({ matches: [] }); responseCallback({ selectors: [] });
} }
}; };
/**
* @async
* @function updateCache
* @description Update cache state
*
* @param {string} [hostname]
* @param {object} [state]
*/
const updateCache = async (hostname, state) => {
chrome.storage.local.get(null, (cache) => {
const current = cache[hostname];
chrome.storage.local.set({
[hostname]: {
enabled:
typeof state.enabled === "undefined"
? current.enabled
: state.enabled,
matches:
typeof state.matches === "undefined"
? current.matches
: [...new Set([...current.matches, ...state.matches])],
},
});
});
};
/** /**
* @description Listens to content messages * @description Listens to content messages
*/ */
@ -156,8 +201,11 @@ chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
case "GET_LIST": case "GET_LIST":
getList(responseCallback); getList(responseCallback);
break; break;
case "GET_TAB":
getTab(responseCallback);
break;
case "UPDATE_CACHE": case "UPDATE_CACHE":
updateCache(request.hostname, request.selector); updateCache(request.hostname, request.state);
break; break;
default: default:
break; break;