"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.runSubProcess=runSubProcess;const getProcess_1=require("./getProcess");let exitTimer,inited=!1;function setExitTimer(e=6e4){-1!==e&&(clearTimeout(exitTimer),exitTimer=setTimeout(()=>{(0,getProcess_1.getProcess)().exit(0)},e))}function processOnMessageFactory(e){return async s=>{if("object"!=typeof s)return void console.error("child process unrecognized message "+s);const{initHandler:t,messageHandler:r}=e;if("init"===s.type){if(inited)return void console.error("native compiler has been inited");try{await t(s),inited=!0,(0,getProcess_1.getProcess)().send({type:"ready"})}catch(e){(0,getProcess_1.getProcess)().send({type:"noReady",error:{code:e.code||-1,message:e.message,stack:e.stack,path:e.path||""}}),(0,getProcess_1.getProcess)().exit(-1e3)}}else inited||console.error("compiler not inited yet"),await r(s);setExitTimer(e.timeout)}}function runSubProcess(e){(0,getProcess_1.getProcess)().on("message",processOnMessageFactory(e)),(0,getProcess_1.getProcess)().on("disconnect",()=>{try{(0,getProcess_1.getProcess)().exit(0)}catch(e){(0,getProcess_1.getProcess)().kill((0,getProcess_1.getProcess)().pid,"SIGTERM")}}),(0,getProcess_1.getProcess)().on("uncaughtException",e=>{console.log(e)})}