Skip to content

Commit

Permalink
Merge pull request #1884 from candela97/fix-storage-init
Browse files Browse the repository at this point in the history
Init storage before processing action callbacks, fix some lints and types
  • Loading branch information
tfedor authored Apr 10, 2024
2 parents 7cc6fc9 + 0b259db commit 5c739cc
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 ITADApiFacade from "../../Content/Modules/Facades/ITADApiFacade";

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

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

0 comments on commit 5c739cc

Please sign in to comment.