UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 558 B
import{PoolItem,pool}from"../pool/index.js";import{Service}from"../service/index.js";export const runServiceWorker=async(e,o,{exceptionHandler:r,...n})=>{const t=new PoolItem({file:e,workerData:{type:1,identifier:o},...n}),i=new Promise(((e,o)=>{t.once("created",(n=>{n.on("error",o);const t=i=>{if(7!==i.type)return;const s=Object.seal(new Service(n));"function"==typeof r&&n.on("message",(async e=>{6===e.type&&await r({error:e.data,terminate:e=>s.close(e)})})),n.off("message",t),n.off("error",o),e(s)};n.on("message",t)}))}));return pool.__enqueue(t),i};