File tree Expand file tree Collapse file tree 3 files changed +9
-4
lines changed Expand file tree Collapse file tree 3 files changed +9
-4
lines changed Original file line number Diff line number Diff line change 1
1
export const taskInfo = Symbol ( 'taskInfo' ) ;
2
2
export const freeWorker = Symbol ( 'freeWorker' ) ;
3
+ export const workerPoolWorkerFlag = 'WorkerPoolWorker' ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { EventEmitter } from 'events';
5
5
6
6
import serializeJavascript from 'serialize-javascript' ;
7
7
8
- import { freeWorker , taskInfo } from './constants' ;
8
+ import { freeWorker , taskInfo , workerPoolWorkerFlag } from './constants' ;
9
9
10
10
import type {
11
11
WorkerCallback ,
@@ -81,7 +81,9 @@ export class WorkerPool extends EventEmitter {
81
81
}
82
82
83
83
private addNewWorker ( ) {
84
- const worker : WorkerWithTaskInfo = new Worker ( this . filePath ) ;
84
+ const worker : WorkerWithTaskInfo = new Worker ( this . filePath , {
85
+ workerData : workerPoolWorkerFlag
86
+ } ) ;
85
87
86
88
worker . on ( 'message' , ( result ) => {
87
89
worker [ taskInfo ] ?. done ( null , result ) ;
Original file line number Diff line number Diff line change 1
- import { isMainThread , parentPort } from 'worker_threads' ;
1
+ import { isMainThread , parentPort , workerData } from 'worker_threads' ;
2
2
3
3
import { hasOwnProperty , isObject } from 'smob' ;
4
4
5
5
import { minify } from 'terser' ;
6
6
7
+ import { workerPoolWorkerFlag } from './constants' ;
8
+
7
9
import type { WorkerContextSerialized , WorkerOutput } from './type' ;
8
10
9
11
/**
@@ -22,7 +24,7 @@ function isWorkerContextSerialized(input: unknown): input is WorkerContextSerial
22
24
}
23
25
24
26
export function runWorker ( ) {
25
- if ( isMainThread || ! parentPort ) {
27
+ if ( isMainThread || ! parentPort || workerData !== workerPoolWorkerFlag ) {
26
28
return ;
27
29
}
28
30
You can’t perform that action at this time.
0 commit comments