simplification / async
This commit is contained in:
parent
c54a900cdd
commit
75e62251fc
141
src/wxBackend.ts
141
src/wxBackend.ts
@ -252,48 +252,37 @@ 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
|
|
||||||
.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 {
|
|
||||||
console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`);
|
console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`);
|
||||||
try {
|
try {
|
||||||
sendResponse({ error: "request unknown" });
|
sendResponse({ error: "request unknown" });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// might fail if tab disconnected
|
// might fail if tab disconnected
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The sendResponse call is sync
|
try {
|
||||||
return false;
|
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 = {};
|
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,57 +506,39 @@ 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);
|
}
|
||||||
})
|
let http = new BrowserHttpLib();
|
||||||
.then((db: IDBDatabase) => {
|
let notifier = new ChromeNotifier();
|
||||||
let http = new BrowserHttpLib();
|
console.log("setting wallet");
|
||||||
let notifier = new ChromeNotifier();
|
wallet = new Wallet(db, http, badge!, notifier);
|
||||||
console.log("setting wallet");
|
|
||||||
wallet = new Wallet(db, http, badge!, notifier);
|
|
||||||
|
|
||||||
// Handlers for messages coming directly from the content
|
// Handlers for messages coming directly from the content
|
||||||
// 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
|
||||||
chrome.webRequest.onHeadersReceived.addListener((details) => {
|
chrome.webRequest.onHeadersReceived.addListener((details) => {
|
||||||
if (details.statusCode == 402) {
|
if (details.statusCode == 402) {
|
||||||
console.log(`got 402 from ${details.url}`);
|
console.log(`got 402 from ${details.url}`);
|
||||||
return handleHttpPayment(details.responseHeaders || [],
|
return handleHttpPayment(details.responseHeaders || [],
|
||||||
details.url,
|
details.url,
|
||||||
details.tabId);
|
details.tabId);
|
||||||
} else if (details.statusCode == 202) {
|
} else if (details.statusCode == 202) {
|
||||||
return handleBankRequest(wallet!, details.responseHeaders || [],
|
return handleBankRequest(wallet!, details.responseHeaders || [],
|
||||||
details.url,
|
details.url,
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user