From 0b259db9cb0637cd847df62de8357540d175a533 Mon Sep 17 00:00:00 2001 From: candela97 <54083835+candela97@users.noreply.github.com> Date: Fri, 29 Mar 2024 00:18:56 +0800 Subject: [PATCH] Init storage before processing action callbacks, fix some lints and types --- src/js/Background/background.ts | 30 +++++++++++----------- src/js/Options/Modules/StoreListBuilder.js | 5 ++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/js/Background/background.ts b/src/js/Background/background.ts index 50735e59c..01040b5a9 100644 --- a/src/js/Background/background.ts +++ b/src/js/Background/background.ts @@ -144,7 +144,7 @@ browser.runtime.onMessage.addListener(( message: Message, sender: MessageSender, sendResponse: (...params: any) => void -): true|void => { +): true|undefined => { if (!sender || !sender.tab) { // not from a tab, ignore return; @@ -153,22 +153,24 @@ browser.runtime.onMessage.addListener(( return; } - (async function() { + (async function(): void { try { + await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]); + let response: any; - switch(message.action) { // TODO rename to "api"? + switch (message.action) { // TODO rename to "api"? case "itad.storelist": response = await ITADApi.getStoreList(); break; - case "prices": - let {country, apps, subs, bundles, voucher, shops} = message.params; + case "prices": { + const {country, apps, subs, bundles, voucher, shops} = (message).params; response = await AugmentedSteamApi.fetchPrices(country, apps, subs, bundles, voucher, shops); - break - - default: + break; + } + default: { /* * TODO deprecated * Old handling, remove once we rewrite all handlers to explicit style above @@ -180,21 +182,19 @@ browser.runtime.onMessage.addListener(( throw new Error(`Did not recognize "${message.action}" as an action.`); } - const params = (message).params || [] - - await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]); - response = await callback(...params); - + const params = (message).params || []; + response = await callback(...params); + } } sendResponse(response); } catch (err) { console.group(`Callback: "${message.action}"`); - console.error('Failed to execute %o', message); + console.error("Failed to execute %o", message); console.error(err); console.groupEnd(); - throw new Error((err).toString()); + throw new Error((err).toString()); } })(); return true; diff --git a/src/js/Options/Modules/StoreListBuilder.js b/src/js/Options/Modules/StoreListBuilder.js index 4fe55dd41..a68686e23 100644 --- a/src/js/Options/Modules/StoreListBuilder.js +++ b/src/js/Options/Modules/StoreListBuilder.js @@ -1,4 +1,5 @@ -import {BackgroundSimple, HTML, SyncedStorage} from "../../modulesCore"; +import {HTML, SyncedStorage} from "../../modulesCore"; +import ITADApiFacade from "../../Content/Modules/Facades/ITADApiFacade"; class StoreListBuilder { @@ -11,7 +12,7 @@ class StoreListBuilder { // This section is re-built on options reset HTML.inner(this._container, ""); - const storeList = await BackgroundSimple.action("itad.storelist").catch(err => console.error(err)); + const storeList = await ITADApiFacade.getStoreList().catch(err => console.error(err)); if (!storeList) { return; } const excludedStores = SyncedStorage.get("excluded_stores");