const eventBuf = [];
onmessage = async ev => {
if (typeof ev.data === 'object' && Reflect.has(ev.data, 'module')) {
const { module, memory, url, wasmInit, init, listen } = ev.data;
const wasmGlue = await import(new URL(url));
if (wasmGlue[wasmInit] === undefined) {
throw new Error('failed to find "' + wasmInit + '" from ' + url);
}
await wasmGlue[wasmInit]({module_or_path: module, memory});
if (wasmGlue[init] === undefined) {
throw new Error('failed to find "' + init + '" from ' + url);
}
wasmGlue[init]();
postMessage(undefined);
const handle = wasmGlue[listen];
if (handle === undefined) {
throw new Error('failed to find "' + listen + '" from ' + url);
}
while (eventBuf.length > 0) {
const ev = eventBuf.shift();
await handle(ev.data);
}
onmessage = async ev => await handle(ev.data);
} else {
eventBuf.push(ev);
}
}