UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 703 B
import{workerData,parentPort}from"worker_threads";import{applyMessengerTransferObjects}from"./utilities.js";export async function taskWorkerHandler(e){process.prependListener("uncaughtException",(e=>{const t={type:6,data:e};parentPort.postMessage(t)}));try{const{name:t,params:r,messengerTransfers:a}=workerData;if(!e?.[t]||"function"!=typeof e[t])throw new Error(`A task with the name ${t} doesn't exist!`);a.length&&applyMessengerTransferObjects(a),await(e.__beforeTask?.({name:t,inService:!1}));const s=await e[t](...r);await(e.__afterTask?.({name:t,inService:!1}));const n={type:1,data:s};parentPort.postMessage(n)}catch(e){const t={type:2,data:e};parentPort.postMessage(t)}finally{process.exit(0)}}