perf: avoid branching on pool type
[poolifier.git] / src / pools / cluster / dynamic.ts
index 9f2f0dd95130a37d4bf010b071dbcc30271ab9ac..08ebc75c76a66ba5ca311c2e269988d741ad5aff 100644 (file)
@@ -1,7 +1,6 @@
-import { type Worker } from 'node:cluster'
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
 import { checkDynamicPoolSize } from '../utils'
-import { FixedClusterPool } from './fixed'
+import { PoolEvents, type PoolType, PoolTypes } from '../pool'
+import { type ClusterPoolOptions, FixedClusterPool } from './fixed'
 
 /**
  * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
@@ -30,7 +29,7 @@ export class DynamicClusterPool<
     min: number,
     max: number,
     filePath: string,
-    opts: PoolOptions<Worker> = {}
+    opts: ClusterPoolOptions = {}
   ) {
     super(min, filePath, opts, max)
     checkDynamicPoolSize(
@@ -39,6 +38,18 @@ export class DynamicClusterPool<
     )
   }
 
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return !this.full && this.internalBusy()
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    if (this.full) {
+      this.emitter?.emit(PoolEvents.full, this.info)
+    }
+  }
+
   /** @inheritDoc */
   protected get type (): PoolType {
     return PoolTypes.dynamic