From dc391be74aedba07618ef0fc9bbce6f997651ab3 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Thu, 23 Mar 2023 20:02:25 +0100 Subject: [PATCH] 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. --- tools/online_editor/src/index.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tools/online_editor/src/index.ts b/tools/online_editor/src/index.ts index 16caf9697..dac081f40 100644 --- a/tools/online_editor/src/index.ts +++ b/tools/online_editor/src/index.ts @@ -22,6 +22,23 @@ import { Widget, } 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 { + return new Promise((res, _) => resolveControllerReady(res, 0)); +} + const lsp_waiter = new LspWaiter(); const commands = new CommandRegistry(); @@ -440,10 +457,12 @@ function setup(lsp: Lsp) { } function main() { - lsp_waiter.wait_for_lsp().then((lsp: Lsp) => { - setup(lsp); - document.body.getElementsByClassName("loader")[0].remove(); - }); + Promise.all([wait_for_service_worker(), lsp_waiter.wait_for_lsp()]).then( + ([_, lsp]) => { + setup(lsp); + document.body.getElementsByClassName("loader")[0].remove(); + }, + ); } window.onload = main;