refactor(back-button): handle busy state better

This commit is contained in:
Manu Mtz.-Almeida
2018-09-14 17:12:22 +02:00
parent 75c2d744b6
commit 9ad05d85cd

View File

@ -26,25 +26,30 @@ export function startHardwareBackButton(win: Window) {
win.document.dispatchEvent(ev);
if (handlers.length > 0) {
busy = true;
let selectedPriority = Number.MIN_SAFE_INTEGER;
let handler: Handler;
let handler: Handler | undefined;
handlers.forEach(h => {
if (h.priority >= selectedPriority) {
selectedPriority = h.priority;
handler = h.handler;
}
});
try {
const result = handler!();
if (result != null) {
result.then(() => (busy = false), () => (busy = false));
} else {
busy = false;
}
} catch (ev) {
busy = false;
}
busy = true;
executeAction(handler).then(() => busy = false);
}
});
}
async function executeAction(handler: Handler | undefined) {
try {
if (handler) {
const result = handler();
if (result) {
await result;
}
}
} catch (e) {
console.error(e);
}
}