online editor: Wait for service worker to be up in index.ts

Wait for the service worker to come up fully. The API is a bit unwieldy,
but we tell to not wait and to claim all clients, so it should come up
the first time round.
This commit is contained in:
Tobias Hunger 2023-03-23 20:02:25 +01:00 committed by Tobias Hunger
parent 4108d49430
commit dc391be74a

View file

@ -22,6 +22,23 @@ import {
Widget, Widget,
} from "@lumino/widgets"; } from "@lumino/widgets";
function resolveControllerReady(resolve: () => void, count: number) {
count += 1;
if (navigator.serviceWorker.controller) {
console.info(`Controller ready after ${count} attempts`);
resolve();
} else {
console.warn(`Controller is not ready yet ! waiting ... - (${count})`);
return setTimeout(() => {
resolveControllerReady(resolve, count);
}, 500);
}
}
function wait_for_service_worker(): Promise<void> {
return new Promise((res, _) => resolveControllerReady(res, 0));
}
const lsp_waiter = new LspWaiter(); const lsp_waiter = new LspWaiter();
const commands = new CommandRegistry(); const commands = new CommandRegistry();
@ -440,10 +457,12 @@ function setup(lsp: Lsp) {
} }
function main() { function main() {
lsp_waiter.wait_for_lsp().then((lsp: Lsp) => { Promise.all([wait_for_service_worker(), lsp_waiter.wait_for_lsp()]).then(
([_, lsp]) => {
setup(lsp); setup(lsp);
document.body.getElementsByClassName("loader")[0].remove(); document.body.getElementsByClassName("loader")[0].remove();
}); },
);
} }
window.onload = main; window.onload = main;