Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web-client: Make client worker initialization more resilient #3245

Open
wants to merge 2 commits into
base: albatross
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions web-client/dist/bundler/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
13 changes: 9 additions & 4 deletions web-client/dist/nodejs/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
13 changes: 9 additions & 4 deletions web-client/dist/nodejs/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
13 changes: 9 additions & 4 deletions web-client/dist/web/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
26 changes: 17 additions & 9 deletions web-client/extras/launcher/client-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ export function clientFactory(workerFactory: () => Worker, comlinkWrapper: (work
async create(config: PlainClientConfiguration): Promise<Client> {
const worker = workerFactory();

// Wait for worker script to load
await new Promise<void>((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<void>((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
Expand Down Expand Up @@ -50,8 +59,7 @@ export function clientFactory(workerFactory: () => Worker, comlinkWrapper: (work
await new Promise<void>((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') {
Expand Down