File tree 1 file changed +11
-1
lines changed
1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter {
833
833
this .numThreads = numThreads;
834
834
this .workers = [];
835
835
this .freeWorkers = [];
836
+ this .tasks = [];
836
837
837
838
for (let i = 0 ; i < numThreads; i++ )
838
839
this .addNewWorker ();
840
+
841
+ // Any time the kWorkerFreedEvent is emitted, dispatch
842
+ // the next task pending in the queue, if any.
843
+ this .on (kWorkerFreedEvent, () => {
844
+ if (this .tasks .length > 0 ) {
845
+ const { task , callback } = this .tasks .shift ();
846
+ this .runTask (task, callback);
847
+ }
848
+ });
839
849
}
840
850
841
851
addNewWorker () {
@@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter {
869
879
runTask (task , callback ) {
870
880
if (this .freeWorkers .length === 0 ) {
871
881
// No free threads, wait until a worker thread becomes free.
872
- this .once (kWorkerFreedEvent, () => this . runTask ( task, callback) );
882
+ this .tasks . push ({ task, callback } );
873
883
return ;
874
884
}
875
885
You can’t perform that action at this time.
0 commit comments