feat(scripts): improve background script documentation and remove matches from cache

This commit is contained in:
wanhose 2021-10-30 14:25:58 +02:00
parent d473180996
commit 96e27a9845

View File

@ -1,11 +1,10 @@
/** /**
* @description Cache initial state * @description Cache initial state
* @type {{ enabled: boolean, matches: string[] }} * @type {{ enabled: boolean }}
*/ */
const initial = { const initial = {
enabled: true, enabled: true,
matches: [],
}; };
/** /**
@ -13,10 +12,7 @@ const initial = {
* @param {object} [cache] * @param {object} [cache]
*/ */
const check = (cache) => const check = (cache) => typeof cache.enabled === "boolean";
typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string");
/** /**
* @description Disables icon * @description Disables icon
@ -70,7 +66,7 @@ const enablePopup = (tabId) => {
* @description Retrieves cache state * @description Retrieves cache state
* @param {string} [hostname] * @param {string} [hostname]
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean }>}
*/ */
const getCache = (hostname, callback) => { const getCache = (hostname, callback) => {
@ -92,7 +88,7 @@ const getCache = (hostname, callback) => {
* @async * @async
* @description Retrieves a selectors list * @description Retrieves a selectors list
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ classes: string[] }>}
*/ */
const getClasses = async (callback) => { const getClasses = async (callback) => {
@ -114,7 +110,7 @@ const getClasses = async (callback) => {
* @async * @async
* @description Retrieves a selectors list * @description Retrieves a selectors list
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ selectors: string }>}
*/ */
const getSelectors = async (callback) => { const getSelectors = async (callback) => {
@ -126,7 +122,7 @@ const getSelectors = async (callback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
callback({ selectors: data.split("\n") }); callback({ selectors: data.split("\n").join(",") });
} catch { } catch {
callback({ selectors: [] }); callback({ selectors: [] });
} }
@ -135,7 +131,7 @@ const getSelectors = async (callback) => {
/** /**
* @description Retrieves current tab information * @description Retrieves current tab information
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ id: string, location: string }>} Current tab information * @returns {Promise<{ id: string, location: string }>}
*/ */
const getTab = (callback) => { const getTab = (callback) => {
@ -163,10 +159,6 @@ const updateCache = (hostname, state) => {
typeof state.enabled === "undefined" typeof state.enabled === "undefined"
? current.enabled ? current.enabled
: state.enabled, : state.enabled,
matches:
typeof state.matches === "undefined"
? current.matches
: [...new Set([...current.matches, ...state.matches])],
}, },
}); });
}); });