simplification / async

This commit is contained in:
Florian Dold 2016-11-20 08:58:04 +01:00
parent c54a900cdd
commit 75e62251fc

View File

@ -252,22 +252,25 @@ function makeHandlers(db: IDBDatabase,
} }
function dispatch(handlers: any, req: any, sender: any, sendResponse: any) { async function dispatch(handlers: any, req: any, sender: any, sendResponse: any): Promise<void> {
if (req.type in handlers) { if (!(req.type in handlers)) {
Promise console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`);
.resolve() try {
.then(() => { sendResponse({ error: "request unknown" });
const p = handlers[req.type](req.detail, sender); } catch (e) {
// might fail if tab disconnected
}
}
return p.then((r: any) => { try {
const p = handlers[req.type](req.detail, sender);
let r = await p;
try { try {
sendResponse(r); sendResponse(r);
} catch (e) { } catch (e) {
// might fail if tab disconnected // might fail if tab disconnected
} }
}) } catch (e) {
})
.catch((e) => {
console.log(`exception during wallet handler for '${req.type}'`); console.log(`exception during wallet handler for '${req.type}'`);
console.log("request", req); console.log("request", req);
console.error(e); console.error(e);
@ -277,23 +280,9 @@ function dispatch(handlers: any, req: any, sender: any, sendResponse: any) {
hint: e.message, hint: e.message,
stack: e.stack.toString() stack: e.stack.toString()
}); });
} catch (e) { } catch (e) {
// might fail if tab disconnected // might fail if tab disconnected
} }
});
// The sendResponse call is async
return true;
} else {
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;
} }
} }
@ -434,7 +423,7 @@ function clearRateLimitCache() {
rateLimitCache = {}; rateLimitCache = {};
} }
export function wxMain() { export async function wxMain() {
window.onerror = (m, source, lineno, colno, error) => { window.onerror = (m, source, lineno, colno, error) => {
logging.record("error", m + error, undefined, source || "(unknown)", lineno || 0, colno || 0); logging.record("error", m + error, undefined, source || "(unknown)", lineno || 0, colno || 0);
} }
@ -517,15 +506,13 @@ export function wxMain() {
chrome.extension.getBackgroundPage().setInterval(clearRateLimitCache, 5000); chrome.extension.getBackgroundPage().setInterval(clearRateLimitCache, 5000);
Promise.resolve() let db: IDBDatabase;
.then(() => { try {
return openTalerDb(); db = await openTalerDb();
}) } catch (e) {
.catch((e) => { console.error("could not open database", e);
console.error("could not open database"); return;
console.error(e); }
})
.then((db: IDBDatabase) => {
let http = new BrowserHttpLib(); let http = new BrowserHttpLib();
let notifier = new ChromeNotifier(); let notifier = new ChromeNotifier();
console.log("setting wallet"); console.log("setting wallet");
@ -535,19 +522,8 @@ export function wxMain() {
// script on the page // script on the page
let handlers = makeHandlers(db, wallet!); let handlers = makeHandlers(db, wallet!);
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => { chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
try { dispatch(handlers, req, sender, sendResponse);
return dispatch(handlers, req, sender, sendResponse) return true;
} 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;
}
}); });
// Handlers for catching HTTP requests // Handlers for catching HTTP requests
@ -563,11 +539,6 @@ export function wxMain() {
details.tabId); details.tabId);
} }
}, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]); }, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
})
.catch((e) => {
console.error("could not initialize wallet messaging");
console.error(e);
});
} }