@@ -26,6 +26,19 @@ let tsNodeAvailable: boolean | undefined
2626
2727export const defaultPoolSize = cpus ( ) . length
2828
29+ interface Terminable {
30+ terminate ( this : Terminable ) : any
31+ }
32+
33+ // Terminates the workers, empties the workers array, and exits.
34+ const onSignal = ( workers : Terminable [ ] , signal : string ) => {
35+ Promise . all ( workers . map ( worker => worker . terminate ( ) ) ) . then (
36+ ( ) => process . exit ( 0 ) ,
37+ ( ) => process . exit ( 1 ) ,
38+ )
39+ workers . length = 0
40+ }
41+
2942function detectTsNode ( ) {
3043 if ( typeof __non_webpack_require__ === "function" ) {
3144 // Webpack build: => No ts-node required or possible
@@ -94,7 +107,7 @@ function initWorkerThreadsWorker(): ImplementationExport {
94107 ? __non_webpack_require__ ( "worker_threads" ) . Worker
95108 : eval ( "require" ) ( "worker_threads" ) . Worker
96109
97- let allWorkers : Array < typeof NativeWorker > = [ ]
110+ const allWorkers : Array < typeof NativeWorker > = [ ]
98111
99112 class Worker extends NativeWorker {
100113 private mappedEventListeners : WeakMap < EventListener , EventListener >
@@ -135,18 +148,9 @@ function initWorkerThreadsWorker(): ImplementationExport {
135148 }
136149 }
137150
138- const terminateWorkersAndMaster = ( ) => {
139- // we should terminate all workers and then gracefully shutdown self process
140- Promise . all ( allWorkers . map ( worker => worker . terminate ( ) ) ) . then (
141- ( ) => process . exit ( 0 ) ,
142- ( ) => process . exit ( 1 ) ,
143- )
144- allWorkers = [ ]
145- }
146-
147151 // Take care to not leave orphaned processes behind. See #147.
148- process . on ( "SIGINT" , ( ) => terminateWorkersAndMaster ( ) )
149- process . on ( "SIGTERM" , ( ) => terminateWorkersAndMaster ( ) )
152+ process . on ( "SIGINT" , ( signal ) => onSignal ( allWorkers , signal ) )
153+ process . on ( "SIGTERM" , ( signal ) => onSignal ( allWorkers , signal ) )
150154
151155 class BlobWorker extends Worker {
152156 constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
@@ -215,19 +219,10 @@ function initTinyWorker(): ImplementationExport {
215219 }
216220 }
217221
218- const terminateWorkersAndMaster = ( ) => {
219- // we should terminate all workers and then gracefully shutdown self process
220- Promise . all ( allWorkers . map ( worker => worker . terminate ( ) ) ) . then (
221- ( ) => process . exit ( 0 ) ,
222- ( ) => process . exit ( 1 ) ,
223- )
224- allWorkers = [ ]
225- }
226-
227222 // Take care to not leave orphaned processes behind
228223 // See <https://github.com/avoidwork/tiny-worker#faq>
229- process . on ( "SIGINT" , ( ) => terminateWorkersAndMaster ( ) )
230- process . on ( "SIGTERM" , ( ) => terminateWorkersAndMaster ( ) )
224+ process . on ( "SIGINT" , ( signal ) => onSignal ( allWorkers , signal ) )
225+ process . on ( "SIGTERM" , ( signal ) => onSignal ( allWorkers , signal ) )
231226
232227 class BlobWorker extends Worker {
233228 constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
0 commit comments