Merge dependabot/npm_and_yarn/examples/typescript/http-client-pool/poolifier-3.1...
[poolifier.git] / src / pools / thread / dynamic.ts
index 119e556dd2ccb454fec3ed9c89c20df5d76199c2..836cae0b0615e1f74733e09f9f21b450bf15506b 100644 (file)
@@ -1,6 +1,6 @@
-import { type PoolType, PoolTypes } from '../pool'
-import { checkDynamicPoolSize } from '../utils'
-import { FixedThreadPool, type ThreadPoolOptions } from './fixed'
+import { PoolEvents, type PoolType, PoolTypes } from '../pool.js'
+import { checkDynamicPoolSize } from '../utils.js'
+import { FixedThreadPool, type ThreadPoolOptions } from './fixed.js'
 
 /**
  * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
@@ -27,12 +27,31 @@ export class DynamicThreadPool<
    */
   public constructor (
     min: number,
-    protected readonly max: number,
+    max: number,
     filePath: string,
     opts: ThreadPoolOptions = {}
   ) {
-    super(min, filePath, opts)
-    checkDynamicPoolSize(this.numberOfWorkers, this.max)
+    super(min, filePath, opts, max)
+    checkDynamicPoolSize(
+      this.minimumNumberOfWorkers,
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      this.maximumNumberOfWorkers!
+    )
+  }
+
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return (
+      (!this.full && this.internalBusy()) ||
+      (this.minimumNumberOfWorkers === 0 && this.workerNodes.length === 0)
+    )
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    if (this.full) {
+      this.emitter?.emit(PoolEvents.full, this.info)
+    }
   }
 
   /** @inheritDoc */