UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 662 B
import{PoolItem,pool}from"../pool/index.js";export const runTaskWorker=(e,r,{name:o,params:a,...t})=>{if(!o)throw new Error("No task name provided!");const n=new PoolItem({file:e,workerData:{type:0,name:o,params:a??[],identifier:r},...t}),i=new Promise(((e,r)=>{n.once("created",(o=>{o.on("error",r),o.on("messageerror",r);const a=o=>{if(0!==o)return r(new Error(`Worker exited early with code ${o}!`));e(void 0)};o.once("exit",a),o.on("message",(async t=>{switch(t.type){case 6:o.off("exit",a),await o.terminate(),r(t.data);break;case 1:o.off("exit",a),e(t.data);break;case 2:o.off("exit",a),r(t.data);break;default:return}}))}))}));return pool.__enqueue(n),i};