summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-20 08:58:04 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-20 08:58:04 +0100
commit75e62251fc8580325fa5986db75086fd1c350bfa (patch)
tree1940a7306841b036f104c95bf49a393d617ac2ed /src
parentc54a900cdd876edb9aad7123c1c89adb874586aa (diff)
downloadwallet-core-75e62251fc8580325fa5986db75086fd1c350bfa.tar.gz
wallet-core-75e62251fc8580325fa5986db75086fd1c350bfa.tar.bz2
wallet-core-75e62251fc8580325fa5986db75086fd1c350bfa.zip
simplification / async
Diffstat (limited to 'src')
-rw-r--r--src/wxBackend.ts143
1 files changed, 57 insertions, 86 deletions
diff --git a/src/wxBackend.ts b/src/wxBackend.ts
index dc3045e3b..b519cd33a 100644
--- a/src/wxBackend.ts
+++ b/src/wxBackend.ts
@@ -252,48 +252,37 @@ function makeHandlers(db: IDBDatabase,
}
-function dispatch(handlers: any, req: any, sender: any, sendResponse: any) {
- if (req.type in handlers) {
- Promise
- .resolve()
- .then(() => {
- const p = handlers[req.type](req.detail, sender);
-
- return p.then((r: any) => {
- try {
- sendResponse(r);
- } catch (e) {
- // might fail if tab disconnected
- }
- })
- })
- .catch((e) => {
- console.log(`exception during wallet handler for '${req.type}'`);
- console.log("request", req);
- console.error(e);
- try {
- sendResponse({
- error: "exception",
- hint: e.message,
- stack: e.stack.toString()
- });
-
- } catch (e) {
- // might fail if tab disconnected
- }
- });
- // The sendResponse call is async
- return true;
- } else {
+async function dispatch(handlers: any, req: any, sender: any, sendResponse: any): Promise<void> {
+ if (!(req.type in handlers)) {
console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`);
try {
sendResponse({ error: "request unknown" });
} catch (e) {
// might fail if tab disconnected
}
+ }
- // The sendResponse call is sync
- return false;
+ try {
+ const p = handlers[req.type](req.detail, sender);
+ let r = await p;
+ try {
+ sendResponse(r);
+ } catch (e) {
+ // might fail if tab disconnected
+ }
+ } catch (e) {
+ console.log(`exception during wallet handler for '${req.type}'`);
+ console.log("request", req);
+ console.error(e);
+ try {
+ sendResponse({
+ error: "exception",
+ hint: e.message,
+ stack: e.stack.toString()
+ });
+ } catch (e) {
+ // might fail if tab disconnected
+ }
}
}
@@ -434,7 +423,7 @@ function clearRateLimitCache() {
rateLimitCache = {};
}
-export function wxMain() {
+export async function wxMain() {
window.onerror = (m, source, lineno, colno, error) => {
logging.record("error", m + error, undefined, source || "(unknown)", lineno || 0, colno || 0);
}
@@ -517,57 +506,39 @@ export function wxMain() {
chrome.extension.getBackgroundPage().setInterval(clearRateLimitCache, 5000);
- Promise.resolve()
- .then(() => {
- return openTalerDb();
- })
- .catch((e) => {
- console.error("could not open database");
- console.error(e);
- })
- .then((db: IDBDatabase) => {
- let http = new BrowserHttpLib();
- let notifier = new ChromeNotifier();
- console.log("setting wallet");
- wallet = new Wallet(db, http, badge!, notifier);
-
- // Handlers for messages coming directly from the content
- // script on the page
- let handlers = makeHandlers(db, wallet!);
- chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
- try {
- return dispatch(handlers, req, sender, sendResponse)
- } catch (e) {
- console.log(`exception during wallet handler (dispatch)`);
- console.log("request", req);
- console.error(e);
- sendResponse({
- error: "exception",
- hint: e.message,
- stack: e.stack.toString()
- });
- return false;
- }
- });
+ let db: IDBDatabase;
+ try {
+ db = await openTalerDb();
+ } catch (e) {
+ console.error("could not open database", e);
+ return;
+ }
+ let http = new BrowserHttpLib();
+ let notifier = new ChromeNotifier();
+ console.log("setting wallet");
+ wallet = new Wallet(db, http, badge!, notifier);
+
+ // Handlers for messages coming directly from the content
+ // script on the page
+ let handlers = makeHandlers(db, wallet!);
+ chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
+ dispatch(handlers, req, sender, sendResponse);
+ return true;
+ });
- // Handlers for catching HTTP requests
- chrome.webRequest.onHeadersReceived.addListener((details) => {
- if (details.statusCode == 402) {
- console.log(`got 402 from ${details.url}`);
- return handleHttpPayment(details.responseHeaders || [],
- details.url,
- details.tabId);
- } else if (details.statusCode == 202) {
- return handleBankRequest(wallet!, details.responseHeaders || [],
- details.url,
- details.tabId);
- }
- }, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
- })
- .catch((e) => {
- console.error("could not initialize wallet messaging");
- console.error(e);
- });
+ // Handlers for catching HTTP requests
+ chrome.webRequest.onHeadersReceived.addListener((details) => {
+ if (details.statusCode == 402) {
+ console.log(`got 402 from ${details.url}`);
+ return handleHttpPayment(details.responseHeaders || [],
+ details.url,
+ details.tabId);
+ } else if (details.statusCode == 202) {
+ return handleBankRequest(wallet!, details.responseHeaders || [],
+ details.url,
+ details.tabId);
+ }
+ }, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
}