JSFiddle - React, Tailwind, and code Playground
by Jason Miller
JavaScript
function comlinker(func) {
/*
return Comlink.wrap(new Worker(URL.createObjectURL(new Blob(['importScripts("https://unpkg.com/comlink@4.2.0/dist/umd/comlink.min.js");var exports={},module={exports:exports};('+func+')(exports);Comlink.expose(module.exports);']))));
*/
return Comlink.wrap(new Worker(URL.createObjectURL(new Blob(['var Comlink='+Comlink.Comlink+'(),exports={},module={exports:exports};('+func+')(exports);Comlink.expose(module.exports);']))));
}
// An inlined version of Comlink that also exposes itself as a factory
// https://unpkg.com/comlink@4.2.0/dist/umd/comlink.min.js
const Comlink = (function Comlink() {const t=Symbol("Comlink.proxy"),n=Symbol("Comlink.endpoint"),r=Symbol("Comlink.releaseProxy"),a=new WeakSet,s=new Map([["proxy",{canHandle:e=>e&&e[t],serialize(e){const{port1:t,port2:n}=new MessageChannel;return o(e,t),[n,[n]]},deserialize:e=>(e.start(),c(e))}],["throw",{canHandle:e=>a.has(e),serialize(e){const t=e instanceof Error;let n=e;return t&&(n={isError:t,message:e.message,stack:e.stack}),[n,[]]},deserialize(e){if(e.isError)throw Object.assign(new Error,e);throw e}}]]);function o(e,t=self){t.addEventListener("message",(function n(r){if(!r||!r.data)return;const{id:s,type:c,path:u}=Object.assign({path:[]},r.data),p=(r.data.argumentList||[]).map(h);let d;try{const t=u.slice(0,-1).reduce((e,t)=>e[t],e),n=u.reduce((e,t)=>e[t],e);switch(c){case 0:d=n;break;case 1:t[u.slice(-1)[0]]=h(r.data.value),d=!0;break;case 2:d=n.apply(t,p);break;case 3:d=f(new n(...p));break;case 4:{const{port1:t,port2:n}=new MessageChannel;o(e,n),d=l(t,[t])}break;case 5:d=void 0}}catch(e){d=e,a.add(e)}Promise.resolve(d).catch(e=>(a.add(e),e)).then(e=>{const[r,a]=m(e);t.postMessage(Object.assign(Object.assign({},r),{id:s}),a),5===c&&(t.removeEventListener("message",n),i(t))})})),t.start&&t.start()}function i(e){(function(e){return"MessagePort"===e.constructor.name})(e)&&e.close()}function c(e,t){return function e(t,a=[],s=function(){}){let o=!1;const c=new...