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); win.document.dispatchEvent(ev);
if (handlers.length > 0) { if (handlers.length > 0) {
busy = true;
let selectedPriority = Number.MIN_SAFE_INTEGER; let selectedPriority = Number.MIN_SAFE_INTEGER;
let handler: Handler; let handler: Handler | undefined;
handlers.forEach(h => { handlers.forEach(h => {
if (h.priority >= selectedPriority) { if (h.priority >= selectedPriority) {
selectedPriority = h.priority; selectedPriority = h.priority;
handler = h.handler; handler = h.handler;
} }
}); });
try {
const result = handler!(); busy = true;
if (result != null) { executeAction(handler).then(() => busy = false);
result.then(() => (busy = false), () => (busy = false));
} else {
busy = false;
}
} catch (ev) {
busy = false;
}
} }
}); });
} }
async function executeAction(handler: Handler | undefined) {
try {
if (handler) {
const result = handler();
if (result) {
await result;
}
}
} catch (e) {
console.error(e);
}
}