perf: avoid branching on pool type
[poolifier.git] / src / pools / cluster / dynamic.ts
index 99a89966e914507fa6868883a0f67c751c8b29c6..08ebc75c76a66ba5ca311c2e269988d741ad5aff 100644 (file)
@@ -1,5 +1,5 @@
-import { type PoolType, PoolTypes } from '../pool'
 import { checkDynamicPoolSize } from '../utils'
+import { PoolEvents, type PoolType, PoolTypes } from '../pool'
 import { type ClusterPoolOptions, FixedClusterPool } from './fixed'
 
 /**
@@ -27,12 +27,27 @@ export class DynamicClusterPool<
    */
   public constructor (
     min: number,
-    protected readonly max: number,
+    max: number,
     filePath: string,
     opts: ClusterPoolOptions = {}
   ) {
-    super(min, filePath, opts)
-    checkDynamicPoolSize(this.numberOfWorkers, this.max)
+    super(min, filePath, opts, max)
+    checkDynamicPoolSize(
+      this.minimumNumberOfWorkers,
+      this.maximumNumberOfWorkers as number
+    )
+  }
+
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return !this.full && this.internalBusy()
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    if (this.full) {
+      this.emitter?.emit(PoolEvents.full, this.info)
+    }
   }
 
   /** @inheritDoc */