Merge dependabot/npm_and_yarn/examples/typescript/http-client-pool/poolifier-3.1...
[poolifier.git] / src / pools / thread / dynamic.ts
index cd9d23253a040f44070da2fe0b0504da31aeb283..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.
@@ -34,10 +34,26 @@ export class DynamicThreadPool<
     super(min, filePath, opts, max)
     checkDynamicPoolSize(
       this.minimumNumberOfWorkers,
-      this.maximumNumberOfWorkers as number
+      // 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 */
   protected get type (): PoolType {
     return PoolTypes.dynamic