@@ -231,31 +231,40 @@ class ThreadPool {
231231 }
232232
233233 _addNewWorker ( ) : void {
234- if ( this . closingUp ) return ;
235-
236- const pool = this ;
237- const worker = new Worker ( resolve ( __dirname , 'worker.js' ) , {
238- env : this . options . env ,
239- argv : this . options . argv ,
240- execArgv : this . options . execArgv ,
241- resourceLimits : this . options . resourceLimits ,
242- workerData : this . options . workerData ,
243- trackUnmanagedFds : this . options . trackUnmanagedFds
244- } ) ;
234+ if ( this . closingUp === true ) return ;
245235
246236 const { port1, port2 } = new MessageChannel ( ) ;
247237 const workerInfo = new WorkerInfo ( {
248- worker,
249- onMessage,
238+ worker : {
239+ filename : resolve ( __dirname , 'worker.js' ) ,
240+ env : this . options . env ,
241+ argv : this . options . argv ,
242+ execArgv : this . options . execArgv ,
243+ resourceLimits : this . options . resourceLimits ,
244+ workerData : this . options . workerData ,
245+ trackUnmanagedFds : this . options . trackUnmanagedFds
246+ } ,
250247 port : port1 ,
251248 enableHistogram : this . options . workerHistogram
252- } ) ;
253-
249+ } , onMessage . bind ( this ) ) ;
250+ const message : StartupMessage = {
251+ filename : this . options . filename ,
252+ name : this . options . name ,
253+ port : port2 ,
254+ sharedBuffer : workerInfo . sharedBuffer ,
255+ atomics : this . options . atomics ! ,
256+ niceIncrement : this . options . niceIncrement
257+ } ;
258+
254259 workerInfo . onDestroy ( ( ) => {
255260 this . publicInterface . emit ( 'workerDestroy' , workerInfo . interface ) ;
256261 } ) ;
262+ workerInfo . onWorkerMessage ( onWorkerMessage . bind ( this ) ) ;
263+ workerInfo . onWorkerError ( onWorkerError . bind ( this ) ) ;
264+ workerInfo . onWorkerExit ( onWorkerExit . bind ( this ) ) ;
265+ workerInfo . onPortClose ( ( ) => { workerInfo . workerRef ( ) ; } ) ;
257266
258- if ( this . startingUp ) {
267+ if ( this . startingUp === true ) {
259268 // There is no point in waiting for the initial set of Workers to indicate
260269 // that they are ready, we just mark them as such from the start.
261270 workerInfo . markAsReady ( ) ;
@@ -271,74 +280,55 @@ class ThreadPool {
271280 this . _onWorkerReady ( workerInfo ) ;
272281 } ) ;
273282 }
283+
284+ workerInfo . init ( message , [ port2 ] ) . workerUnref ( ) ;
285+ this . workers . add ( workerInfo ) ;
274286
275- const message : StartupMessage = {
276- filename : this . options . filename ,
277- name : this . options . name ,
278- port : port2 ,
279- sharedBuffer : workerInfo . sharedBuffer ,
280- atomics : this . options . atomics ! ,
281- niceIncrement : this . options . niceIncrement
282- } ;
283- worker . postMessage ( message , [ port2 ] ) ;
284-
285- function onMessage ( message : ResponseMessage ) {
287+ function onMessage ( this : ThreadPool , message : ResponseMessage ) {
286288 const { taskId, result } = message ;
287289 // In case of success: Call the callback that was passed to `runTask`,
288290 // remove the `TaskInfo` associated with the Worker, which marks it as
289291 // free again.
290292 const taskInfo = workerInfo . popTask ( taskId ) ;
291- pool . workers . taskDone ( workerInfo ) ;
293+ this . workers . taskDone ( workerInfo ) ;
292294
293- /* istanbul ignore if */
294- if ( taskInfo == null ) {
295+
296+ if ( taskInfo == null ) { /* c8 ignore next */
295297 const err = new Error (
296298 `Unexpected message from Worker: ${ inspect ( message ) } ` ) ;
297- pool . publicInterface . emit ( 'error' , err ) ;
299+ this . publicInterface . emit ( 'error' , err ) ;
298300 } else {
299301 taskInfo . done ( message . error , result ) ;
300302 }
301303
302- pool . _processPendingMessages ( ) ;
304+ this . _processPendingMessages ( ) ;
303305 }
304306
305- function onReady ( ) {
307+ function onWorkerReady ( ) {
306308 workerInfo . currentUsage ( ) === 0 && workerInfo . unref ( ) ;
307309 workerInfo . isReady ( ) === false && workerInfo . markAsReady ( ) ;
308310 }
309311
310- function onEventMessage ( message : any ) {
311- pool . publicInterface . emit ( 'message' , message ) ;
312+ function onEventMessage ( this : ThreadPool , message : any ) {
313+ this . publicInterface . emit ( 'message' , message ) ;
312314 }
313315
314- worker . on ( 'message' , ( message : any ) => {
315- message instanceof Object && READY in message ? onReady ( ) : onEventMessage ( message ) ;
316- } ) ;
316+ function onWorkerMessage ( this : ThreadPool , message : any ) {
317+ message instanceof Object && READY in message ? onWorkerReady ( ) : onEventMessage . call ( this , message ) ;
318+ }
317319
318- worker . on ( 'error' , ( err : Error ) => {
320+ function onWorkerError ( this : ThreadPool , err : Error ) {
319321 this . _onError ( workerInfo , err , false ) ;
320- } ) ;
322+ }
321323
322- worker . on ( 'exit' , ( exitCode : number ) => {
323- if ( this . destroying ) {
324- return ;
324+ function onWorkerExit ( this : ThreadPool , code : number ) {
325+ if ( this . destroying === false ) {
326+ const err = new Error ( `worker exited with code: ${ code } ` ) ;
327+ // Only error unfinished tasks on process exit, since there are legitimate
328+ // reasons to exit workers and we want to handle that gracefully when possible.
329+ this . _onError ( workerInfo , err , true ) ;
325330 }
326-
327- const err = new Error ( `worker exited with code: ${ exitCode } ` ) ;
328- // Only error unfinished tasks on process exit, since there are legitimate
329- // reasons to exit workers and we want to handle that gracefully when possible.
330- this . _onError ( workerInfo , err , true ) ;
331- } ) ;
332-
333- worker . unref ( ) ;
334- port1 . on ( 'close' , ( ) => {
335- // The port is only closed if the Worker stops for some reason, but we
336- // always .unref() the Worker itself. We want to receive e.g. 'error'
337- // events on it, so we ref it once we know it's going to exit anyway.
338- worker . ref ( ) ;
339- } ) ;
340-
341- this . workers . add ( workerInfo ) ;
331+ }
342332 }
343333
344334 _onError ( workerInfo : WorkerInfo , err : Error , onlyErrorUnfinishedTasks : boolean ) {
0 commit comments