Skip to content

Commit

Permalink
Init storage before processing action callbacks, fix some lints and t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
candela97 committed Mar 28, 2024
1 parent b867b9c commit ed118f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
30 changes: 15 additions & 15 deletions src/js/Background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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} = (<TFetchPricesMessage>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
Expand All @@ -180,21 +182,19 @@ browser.runtime.onMessage.addListener((
throw new Error(`Did not recognize "${message.action}" as an action.`);
}

const params = (<GenericMessage>message).params || []

await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]);
response = await callback(...params);

const params = (<GenericMessage>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((<any>err).toString());
throw new Error((<Error>err).toString());
}
})();
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/js/Options/Modules/StoreListBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BackgroundSimple, HTML, SyncedStorage} from "../../modulesCore";
import {HTML, SyncedStorage} from "../../modulesCore";
import {ITADApi} from "../Background/Modules/IsThereAnyDeal/ITADApi";

class StoreListBuilder {

Expand All @@ -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 ITADApi.getStoreList().catch(err => console.error(err));
if (!storeList) { return; }

const excludedStores = SyncedStorage.get("excluded_stores");
Expand Down

0 comments on commit ed118f4

Please sign in to comment.