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 @@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter {
833833 this .numThreads = numThreads;
834834 this .workers = [];
835835 this .freeWorkers = [];
836+ this .tasks = [];
836837
837838 for (let i = 0 ; i < numThreads; i++ )
838839 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+ });
839849 }
840850
841851 addNewWorker () {
@@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter {
869879 runTask (task , callback ) {
870880 if (this .freeWorkers .length === 0 ) {
871881 // No free threads, wait until a worker thread becomes free.
872- this .once (kWorkerFreedEvent, () => this . runTask ( task, callback) );
882+ this .tasks . push ({ task, callback } );
873883 return ;
874884 }
875885
You can’t perform that action at this time.
0 commit comments