diff --git a/web-client/dist/bundler/worker.js b/web-client/dist/bundler/worker.js index 7298abc72b..a4a84496d2 100644 --- a/web-client/dist/bundler/worker.js +++ b/web-client/dist/bundler/worker.js @@ -32,10 +32,16 @@ async function init(config) { }; self.addEventListener('message', async (event) => { - const { type } = event.data; + const data = event.data; + + if (data === 'NIMIQ_CHECKREADY') { + self.postMessage('NIMIQ_READY'); + return; + } + + const { type, config } = data; if (type !== 'NIMIQ_INIT') return; - let { config } = event.data; if (!config || typeof config !== 'object') config = {}; try { @@ -46,5 +52,4 @@ self.addEventListener('message', async (event) => { } }); -self.postMessage('NIMIQ_ONLOAD'); -console.debug('Launched client WASM worker, ready for init'); +console.debug('Client WASM worker ready'); diff --git a/web-client/dist/nodejs/worker.js b/web-client/dist/nodejs/worker.js index 4395df3211..ca43b74390 100644 --- a/web-client/dist/nodejs/worker.js +++ b/web-client/dist/nodejs/worker.js @@ -45,10 +45,16 @@ async function init(config) { }; parentPort.addListener('message', async (event) => { - const { type } = event; + const data = event.data; + + if (data === 'NIMIQ_CHECKREADY') { + parentPort.postMessage('NIMIQ_READY'); + return; + } + + const { type, config } = data; if (type !== 'NIMIQ_INIT') return; - let { config } = event; if (!config || typeof config !== 'object') config = {}; try { @@ -59,5 +65,4 @@ parentPort.addListener('message', async (event) => { } }); -parentPort.postMessage('NIMIQ_ONLOAD'); -console.debug('Launched client WASM worker, ready for init'); +console.debug('Client WASM worker ready'); diff --git a/web-client/dist/nodejs/worker.mjs b/web-client/dist/nodejs/worker.mjs index e78edd6a31..61c31c2cc8 100644 --- a/web-client/dist/nodejs/worker.mjs +++ b/web-client/dist/nodejs/worker.mjs @@ -45,10 +45,16 @@ async function init(config) { }; parentPort.addListener('message', async (event) => { - const { type } = event; + const data = event.data; + + if (data === 'NIMIQ_CHECKREADY') { + parentPort.postMessage('NIMIQ_READY'); + return; + } + + const { type, config } = data; if (type !== 'NIMIQ_INIT') return; - let { config } = event; if (!config || typeof config !== 'object') config = {}; try { @@ -59,5 +65,4 @@ parentPort.addListener('message', async (event) => { } }); -parentPort.postMessage('NIMIQ_ONLOAD'); -console.debug('Launched client WASM worker, ready for init'); +console.debug('Client WASM worker ready'); diff --git a/web-client/dist/web/worker.js b/web-client/dist/web/worker.js index b8dc0a61fc..f2731671c9 100644 --- a/web-client/dist/web/worker.js +++ b/web-client/dist/web/worker.js @@ -39,10 +39,16 @@ async function init(config) { }; self.addEventListener('message', async (event) => { - const { type } = event.data; + const data = event.data; + + if (data === 'NIMIQ_CHECKREADY') { + self.postMessage('NIMIQ_READY'); + return; + } + + const { type, config } = data; if (type !== 'NIMIQ_INIT') return; - let { config } = event.data; if (!config || typeof config !== 'object') config = {}; try { @@ -53,5 +59,4 @@ self.addEventListener('message', async (event) => { } }); -self.postMessage('NIMIQ_ONLOAD'); -console.debug('Launched client WASM worker, ready for init'); +console.debug('Client WASM worker ready'); diff --git a/web-client/extras/launcher/client-proxy.ts b/web-client/extras/launcher/client-proxy.ts index fcb44c3c0a..c4cd1b44bf 100644 --- a/web-client/extras/launcher/client-proxy.ts +++ b/web-client/extras/launcher/client-proxy.ts @@ -9,14 +9,23 @@ export function clientFactory(workerFactory: () => Worker, comlinkWrapper: (work async create(config: PlainClientConfiguration): Promise { const worker = workerFactory(); - // Wait for worker script to load - await new Promise((resolve) => { - const readyListener = (event: {} | MessageEvent) => { - removeEventListener(worker, 'message', readyListener); - if (getEventData(event) === 'NIMIQ_ONLOAD') resolve(); - }; - addEventListener(worker, 'message', readyListener); + // Wait for worker script to be ready + let workerReady: () => void; + const readyPromise = new Promise((resolve) => { + workerReady = resolve; }); + const readyListener = (event: {} | MessageEvent) => { + if (getEventData(event) === 'NIMIQ_READY') { + workerReady(); + } + }; + addEventListener(worker, 'message', readyListener); + const readyCheckInterval = setInterval(() => { + worker.postMessage('NIMIQ_CHECKREADY'); + }, 20); + await readyPromise; + removeEventListener(worker, 'message', readyListener); + clearInterval(readyCheckInterval); console.debug('Client WASM worker loaded'); // Wrap the worker with Comlink, to transparently proxy any method calls on the client @@ -50,8 +59,7 @@ export function clientFactory(workerFactory: () => Worker, comlinkWrapper: (work await new Promise((resolve, reject) => { addEventListener(worker, 'message', (event) => { const eventData = getEventData(event); - - if (!('ok' in eventData)) return; + if (typeof eventData !== 'object' || !('ok' in eventData)) return; if (eventData.ok === true) resolve(); if (eventData.ok === false && 'error' in eventData && typeof eventData.error === 'string') {