A tiny RPC library for windows, iframes, WebWorkers and ServiceWorkers.
comlinkjs has the below interface, makes use of MessageChannel helps you to work on objects from another JavaScript realm (like a Worker or an iframe) as if it was a local object. Just use await whenever the remote value is involed.
// On the site:constworker=newWorker('worker.js');// WebWorkers use `postMessage` and therefore work with Comlink.constapi=Comlink.proxy(worker);(asyncfunctioninit(){// Note the usage of `await`:constapp=awaitnewapi.App();console.log(`Counter:${awaitapp.count}`);awaitapp.inc();console.log(`Counter:${awaitapp.count}`);}());
12345678910111213141516
// In the workerclassApp{constructor(){this._counter=0;}getcount(){returnthis._counter;}inc(){this._counter++;}}Comlink.expose({App},self);