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) {
if (req.type in handlers) {
Promise
.resolve()
.then(() => {
const p = handlers[req.type](req.detail, sender);
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
}
}
return p.then((r: any) => {
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) => {
} catch (e) {
console.log(`exception during wallet handler for '${req.type}'`);
console.log("request", req);
console.error(e);
@ -277,23 +280,9 @@ function dispatch(handlers: any, req: any, sender: any, sendResponse: any) {
hint: e.message,
stack: e.stack.toString()
});
} catch (e) {
// 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 = {};
}
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,15 +506,13 @@ 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 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");
@ -535,19 +522,8 @@ export function wxMain() {
// 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;
}
dispatch(handlers, req, sender, sendResponse);
return true;
});
// Handlers for catching HTTP requests
@ -563,11 +539,6 @@ export function wxMain() {
details.tabId);
}
}, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
})
.catch((e) => {
console.error("could not initialize wallet messaging");
console.error(e);
});
}