import { workerData, parentPort } from 'worker_threads' import initLog from '../log.js' let runScript = '' let myName = '' let log = initLog(myName) parentPort.on( 'message', async message => { switch (message.type) { case 'initWorker': myName = message.name log = initLog(myName) process.env.DEBUG && log(`Started new thread -> «${myName}»`) break case 'runScript': runScript = message.script process.env.DEBUG && log(`Running script -> «${runScript}»`) import(runScript).then( script => script.main(message.data).then( result => { parentPort.postMessage({ result, id: message.id }) process.env.DEBUG && log(`Script done -> «${runScript}»`) } ) ) break default: log(`Invalid message -> ${message.type ?? 'No message type provided!'}`) break } } )