import type * as d from '../../declarations' ; export const initNodeWorkerThread = ( prcs , msgHandler ) => { const sendHandle = ( err ) => { if ( err && err . code === 'ERR_IPC_CHANNEL_CLOSED' ) { prcs . exit ( 0 ) ; } } ; const errorHandler = ( stencilMsgId , err ) => { const errMsgBackToMain = { stencilId : stencilMsgId , stencilRtnValue : null , stencilRtnError : 'Error' , } ; if ( typeof err === 'string' ) { errMsgBackToMain . stencilRtnError += ': ' + err ; } else if ( err ) { if ( err . stack ) { errMsgBackToMain . stencilRtnError += ': ' + err . stack ; } else if ( err . message ) { errMsgBackToMain . stencilRtnError += ':' + err . message ; } } prcs . send ( errMsgBackToMain , sendHandle ) ; } ; prcs . on ( 'message' , async ( msgToWorker ) => { if ( msgToWorker && typeof msgToWorker . stencilId === 'number' ) { try { const msgFromWorker = { stencilId : msgToWorker . stencilId , stencilRtnValue : await msgHandler ( msgToWorker ) , stencilRtnError : null , } ; prcs . send ( msgFromWorker , sendHandle ) ; } catch ( e ) { errorHandler ( msgToWorker . stencilId , e ) ; } } } ) ; prcs . on ( `unhandledRejection` , ( e ) => { errorHandler ( - 1 , e ) ; } ) ; } ;