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 @@ -839,9 +839,19 @@ class WorkerPool extends EventEmitter {
839
839
this .numThreads = numThreads;
840
840
this .workers = [];
841
841
this .freeWorkers = [];
842
+ this .tasks = [];
842
843
843
844
for (let i = 0 ; i < numThreads; i++ )
844
845
this .addNewWorker ();
846
+
847
+ // Any time the kWorkerFreedEvent is emitted, dispatch
848
+ // the next task pending in the queue, if any.
849
+ this .on (kWorkerFreedEvent, () => {
850
+ if (this .tasks .length > 0 ) {
851
+ const { task , callback } = this .tasks .shift ();
852
+ this .runTask (task, callback);
853
+ }
854
+ });
845
855
}
846
856
847
857
addNewWorker () {
@@ -875,7 +885,7 @@ class WorkerPool extends EventEmitter {
875
885
runTask (task , callback ) {
876
886
if (this .freeWorkers .length === 0 ) {
877
887
// No free threads, wait until a worker thread becomes free.
878
- this .once (kWorkerFreedEvent, () => this . runTask ( task, callback) );
888
+ this .tasks . push ({ task, callback } );
879
889
return ;
880
890
}
881
891
You can’t perform that action at this time.
0 commit comments