UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 888 B
import{isMainThread,workerData}from"worker_threads";import{workerHandler}from"../handlers/index.js";import{runTaskWorker,runServiceWorker}from"../runners/index.js";import{assertCurrentFileNotEqual,getAutoIdentifier,getCurrentFile}from"./utilities.js";import{ServiceCluster}from"../service_cluster/index.js";export async function define(e,{identifier:r=getAutoIdentifier(e),file:t,safeMode:i=!0}={}){const n=t??getCurrentFile(),s=Object.freeze(Object.assign((async e=>(i&&assertCurrentFileNotEqual(n),runTaskWorker(n,r,e))),{launchService:Object.freeze((async(e={})=>(i&&assertCurrentFileNotEqual(n),runServiceWorker(n,r,e)))),clusterize:Object.freeze((async function(e=1,r={}){i&&assertCurrentFileNotEqual(n);const t=new ServiceCluster(this,{autoRenew:r.autoRenew});return await t.launch(e,r),t})),file:n,identifier:r}));return isMainThread||workerData.identifier!==r||workerHandler(e),s}