File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 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 {
839839 this .numThreads = numThreads;
840840 this .workers = [];
841841 this .freeWorkers = [];
842+ this .tasks = [];
842843
843844 for (let i = 0 ; i < numThreads; i++ )
844845 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+ });
845855 }
846856
847857 addNewWorker () {
@@ -875,7 +885,7 @@ class WorkerPool extends EventEmitter {
875885 runTask (task , callback ) {
876886 if (this .freeWorkers .length === 0 ) {
877887 // No free threads, wait until a worker thread becomes free.
878- this .once (kWorkerFreedEvent, () => this . runTask ( task, callback) );
888+ this .tasks . push ({ task, callback } );
879889 return ;
880890 }
881891
You can’t perform that action at this time.
0 commit comments