fix: properly handle dynamic pool with zero minimum size
[poolifier.git] / src / pools / thread / fixed.ts
index d2f4df8e43f8fe0a905e115510c0c22424891313..c4256bf3b2213d8d7a6a60d7df8a95447576b888 100644 (file)
@@ -1,14 +1,18 @@
 import {
-  type MessageChannel,
   type MessagePort,
   type TransferListItem,
   type Worker,
   isMainThread
 } from 'node:worker_threads'
-import type { MessageValue } from '../../utility-types'
-import { AbstractPool } from '../abstract-pool'
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
-import { type WorkerType, WorkerTypes } from '../worker'
+import type { MessageValue } from '../../utility-types.js'
+import { AbstractPool } from '../abstract-pool.js'
+import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js'
+import { type WorkerType, WorkerTypes } from '../worker.js'
+
+/**
+ * Options for a poolifier thread pool.
+ */
+export type ThreadPoolOptions = PoolOptions<Worker>
 
 /**
  * A thread pool with a fixed number of threads.
@@ -32,7 +36,7 @@ export class FixedThreadPool<
   public constructor (
     numberOfThreads: number,
     filePath: string,
-    opts: PoolOptions<Worker> = {},
+    opts: ThreadPoolOptions = {},
     maximumNumberOfThreads?: number
   ) {
     super(numberOfThreads, filePath, opts, maximumNumberOfThreads)
@@ -58,8 +62,8 @@ export class FixedThreadPool<
   /** @inheritDoc */
   protected sendStartupMessageToWorker (workerNodeKey: number): void {
     const workerNode = this.workerNodes[workerNodeKey]
-    const port2: MessagePort = (workerNode.messageChannel as MessageChannel)
-      .port2
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    const port2: MessagePort = workerNode.messageChannel!.port2
     workerNode.worker.postMessage(
       {
         ready: false,
@@ -103,6 +107,16 @@ export class FixedThreadPool<
     )
   }
 
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return false
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    /* noop */
+  }
+
   /** @inheritDoc */
   protected get type (): PoolType {
     return PoolTypes.fixed