refactor: reorder benchmarks
[poolifier.git] / src / pools / abstract-pool.ts
index 3ef57612416696c5b40305a0686e68a1f41e749c..87427ad719079d1b4462661bcba473f94786127e 100644 (file)
@@ -84,6 +84,10 @@ export abstract class AbstractPool<
   Response
   >
 
+  /**
+   * Whether the pool is starting or not.
+   */
+  private readonly starting: boolean
   /**
    * The start timestamp of the pool.
    */
@@ -128,15 +132,9 @@ export abstract class AbstractPool<
 
     this.setupHook()
 
-    while (
-      this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
-        0
-      ) < this.numberOfWorkers
-    ) {
-      this.createAndSetupWorker()
-    }
+    this.starting = true
+    this.startPool()
+    this.starting = false
 
     this.startTimestamp = performance.now()
   }
@@ -279,6 +277,18 @@ export abstract class AbstractPool<
     }
   }
 
+  private startPool (): void {
+    while (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.numberOfWorkers
+    ) {
+      this.createAndSetupWorker()
+    }
+  }
+
   /** @inheritDoc */
   public get info (): PoolInfo {
     return {
@@ -416,16 +426,6 @@ export abstract class AbstractPool<
     }
   }
 
-  private get starting (): boolean {
-    return (
-      this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
-        0
-      ) < this.minSize
-    )
-  }
-
   private get ready (): boolean {
     return (
       this.workerNodes.reduce(
@@ -943,9 +943,7 @@ export abstract class AbstractPool<
       const workerNodeKey = this.getWorkerNodeKey(worker)
       const workerInfo = this.getWorkerInfo(workerNodeKey)
       workerInfo.ready = false
-      if (this.emitter != null) {
-        this.emitter.emit(PoolEvents.error, error)
-      }
+      this.emitter?.emit(PoolEvents.error, error)
       if (this.opts.restartWorkerOnError === true && !this.starting) {
         if (workerInfo.dynamic) {
           this.createAndSetupDynamicWorker()
@@ -1078,8 +1076,8 @@ export abstract class AbstractPool<
     return message => {
       this.checkMessageWorkerId(message)
       if (message.ready != null) {
-        // Worker ready message received
-        this.handleWorkerReadyMessage(message)
+        // Worker ready response received
+        this.handleWorkerReadyResponse(message)
       } else if (message.id != null) {
         // Task execution response received
         this.handleTaskExecutionResponse(message)
@@ -1087,7 +1085,7 @@ export abstract class AbstractPool<
     }
   }
 
-  private handleWorkerReadyMessage (message: MessageValue<Response>): void {
+  private handleWorkerReadyResponse (message: MessageValue<Response>): void {
     const worker = this.getWorkerById(message.workerId)
     this.getWorkerInfo(this.getWorkerNodeKey(worker as Worker)).ready =
       message.ready as boolean
@@ -1100,9 +1098,7 @@ export abstract class AbstractPool<
     const promiseResponse = this.promiseResponseMap.get(message.id as string)
     if (promiseResponse != null) {
       if (message.taskError != null) {
-        if (this.emitter != null) {
-          this.emitter.emit(PoolEvents.taskError, message.taskError)
-        }
+        this.emitter?.emit(PoolEvents.taskError, message.taskError)
         promiseResponse.reject(message.taskError.message)
       } else {
         promiseResponse.resolve(message.data as Response)