@@ -27,6 +27,19 @@ let tsNodeAvailable: boolean | undefined
27
27
28
28
export const defaultPoolSize = cpus ( ) . length
29
29
30
+ interface Terminable {
31
+ terminate ( this : Terminable ) : any
32
+ }
33
+
34
+ // Terminates the workers, empties the workers array, and exits.
35
+ const onSignal = ( workers : Terminable [ ] , signal : string ) => {
36
+ Promise . all ( workers . map ( worker => worker . terminate ( ) ) ) . then (
37
+ ( ) => process . exit ( 0 ) ,
38
+ ( ) => process . exit ( 1 ) ,
39
+ )
40
+ workers . length = 0
41
+ }
42
+
30
43
function detectTsNode ( ) {
31
44
if ( typeof __non_webpack_require__ === "function" ) {
32
45
// Webpack build: => No ts-node required or possible
@@ -98,7 +111,7 @@ function initWorkerThreadsWorker(): ImplementationExport {
98
111
? __non_webpack_require__ ( "worker_threads" ) . Worker
99
112
: eval ( "require" ) ( "worker_threads" ) . Worker
100
113
101
- let allWorkers : Array < typeof NativeWorker > = [ ]
114
+ const allWorkers : Array < typeof NativeWorker > = [ ]
102
115
103
116
class Worker extends NativeWorker {
104
117
private mappedEventListeners : WeakMap < EventListener , EventListener >
@@ -139,18 +152,9 @@ function initWorkerThreadsWorker(): ImplementationExport {
139
152
}
140
153
}
141
154
142
- const terminateWorkersAndMaster = ( ) => {
143
- // we should terminate all workers and then gracefully shutdown self process
144
- Promise . all ( allWorkers . map ( worker => worker . terminate ( ) ) ) . then (
145
- ( ) => process . exit ( 0 ) ,
146
- ( ) => process . exit ( 1 ) ,
147
- )
148
- allWorkers = [ ]
149
- }
150
-
151
155
// Take care to not leave orphaned processes behind. See #147.
152
- process . on ( "SIGINT" , ( ) => terminateWorkersAndMaster ( ) )
153
- process . on ( "SIGTERM" , ( ) => terminateWorkersAndMaster ( ) )
156
+ process . on ( "SIGINT" , ( signal ) => onSignal ( allWorkers , signal ) )
157
+ process . on ( "SIGTERM" , ( signal ) => onSignal ( allWorkers , signal ) )
154
158
155
159
class BlobWorker extends Worker {
156
160
constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
@@ -219,19 +223,10 @@ function initTinyWorker(): ImplementationExport {
219
223
}
220
224
}
221
225
222
- const terminateWorkersAndMaster = ( ) => {
223
- // we should terminate all workers and then gracefully shutdown self process
224
- Promise . all ( allWorkers . map ( worker => worker . terminate ( ) ) ) . then (
225
- ( ) => process . exit ( 0 ) ,
226
- ( ) => process . exit ( 1 ) ,
227
- )
228
- allWorkers = [ ]
229
- }
230
-
231
226
// Take care to not leave orphaned processes behind
232
227
// See <https://github.com/avoidwork/tiny-worker#faq>
233
- process . on ( "SIGINT" , ( ) => terminateWorkersAndMaster ( ) )
234
- process . on ( "SIGTERM" , ( ) => terminateWorkersAndMaster ( ) )
228
+ process . on ( "SIGINT" , ( signal ) => onSignal ( allWorkers , signal ) )
229
+ process . on ( "SIGTERM" , ( signal ) => onSignal ( allWorkers , signal ) )
235
230
236
231
class BlobWorker extends Worker {
237
232
constructor ( blob : Uint8Array , options ?: ThreadsWorkerOptions ) {
0 commit comments