UNPKG

wasmrs-js

Version:

A JavaScript implementation of the RSocket protocol over WebAssembly.

42 lines 1.45 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import DEBUG from 'debug'; import { WasmRsModule } from '../wasmrs.js'; const debug = DEBUG('wasmrs:worker'); class WorkerInstance { instance; constructor(instance, scope) { this.instance = instance; scope.addEventListener('message', (msg) => { this.handleMessage(msg); }); instance.addEventListener('frame', (e) => { const msg = e; scope.postMessage(msg.payload); }); const setupResponse = { success: true, operations: instance.operations, }; debug('started'); scope.postMessage(setupResponse); } handleMessage(msg) { debug('received frame '); this.instance.send(msg.data); } } export function main(scope) { DEBUG.enabled('wasmrs:worker*'); // using {once:true} is inconsistent between node and browser so we need // to manually add and remove our bound init listener. const init = async (msg) => { scope.removeEventListener('message', init); debug('received init message %o', { wasi: msg.data.wasi }); const mod = WasmRsModule.from(msg.data.module); const instance = await mod.instantiate({ wasi: msg.data.wasi }); new WorkerInstance(instance, scope); }; debug('starting'); scope.addEventListener('message', init); } //# sourceMappingURL=worker.js.map