refactor: reorder benchmarks
[poolifier.git] / src / pools / abstract-pool.ts
index 50ce2026d4f2e17f182f773227c2bc51f22de26f..87427ad719079d1b4462661bcba473f94786127e 100644 (file)
@@ -85,7 +85,7 @@ export abstract class AbstractPool<
   >
 
   /**
-   * Whether the pool is starting.
+   * Whether the pool is starting or not.
    */
   private readonly starting: boolean
   /**
@@ -133,15 +133,7 @@ export abstract class AbstractPool<
     this.setupHook()
 
     this.starting = true
-    while (
-      this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
-        0
-      ) < this.numberOfWorkers
-    ) {
-      this.createAndSetupWorker()
-    }
+    this.startPool()
     this.starting = false
 
     this.startTimestamp = performance.now()
@@ -285,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 {
@@ -939,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()
@@ -1096,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)