@@ -26,6 +26,19 @@ let tsNodeAvailable: boolean | undefined
26
26
27
27
export const defaultPoolSize = cpus ( ) . length
28
28
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
+
29
42
function detectTsNode ( ) {
30
43
if ( typeof __non_webpack_require__ === "function" ) {
31
44
// Webpack build: => No ts-node required or possible
@@ -94,7 +107,7 @@ function initWorkerThreadsWorker(): ImplementationExport {
94
107
? __non_webpack_require__ ( "worker_threads" ) . Worker
95
108
: eval ( "require" ) ( "worker_threads" ) . Worker
96
109
97
- let allWorkers : Array < typeof NativeWorker > = [ ]
110
+ const allWorkers : Array < typeof NativeWorker > = [ ]
98
111
99
112
class Worker extends NativeWorker {
100
113
private mappedEventListeners : WeakMap < EventListener , EventListener >
@@ -135,18 +148,9 @@ function initWorkerThreadsWorker(): ImplementationExport {
135
148
}
136
149
}
137
150
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
-
147
151
// 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 ) )
150
154
151
155
class BlobWorker extends Worker {
152
156
constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
@@ -215,19 +219,10 @@ function initTinyWorker(): ImplementationExport {
215
219
}
216
220
}
217
221
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
-
227
222
// Take care to not leave orphaned processes behind
228
223
// 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 ) )
231
226
232
227
class BlobWorker extends Worker {
233
228
constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
0 commit comments