Removed max tasks (#225)
[poolifier.git] / src / pools / thread / fixed.ts
index 705f124aea4dda5f35234dea3f8d2f1b59e827a6..6fec9aa04db6af79c7eac3deae91d09b4d311cd4 100644 (file)
@@ -1,5 +1,5 @@
 import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads'
-import type { Draft, JSONValue, MessageValue } from '../../utility-types'
+import type { Draft, MessageValue } from '../../utility-types'
 import type { PoolOptions } from '../abstract-pool'
 import { AbstractPool } from '../abstract-pool'
 
@@ -15,27 +15,27 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>
  *
  * This pool selects the threads in a round robin fashion.
  *
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
  *
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
  * @since 0.0.1
  */
 export class FixedThreadPool<
-  Data extends JSONValue = JSONValue,
-  Response extends JSONValue = JSONValue
+  Data = unknown,
+  Response = unknown
 > extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
   /**
    * Constructs a new poolifier fixed thread pool.
    *
    * @param numberOfThreads Number of threads for this pool.
    * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
-   * @param opts Options for this fixed thread pool. Default: `{ maxTasks: 1000 }`
+   * @param opts Options for this fixed thread pool. Default: `{}`
    */
   public constructor (
     numberOfThreads: number,
     filePath: string,
-    opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
+    opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
   ) {
     super(numberOfThreads, filePath, opts)
   }
@@ -44,9 +44,11 @@ export class FixedThreadPool<
     return isMainThread
   }
 
-  protected async destroyWorker (
+  /** @inheritdoc */
+  public async destroyWorker (
     worker: ThreadWorkerWithMessageChannel
   ): Promise<void> {
+    this.sendToWorker(worker, { kill: 1 })
     await worker.terminate()
   }
 
@@ -57,20 +59,14 @@ export class FixedThreadPool<
     worker.postMessage(message)
   }
 
-  protected registerWorkerMessageListener<Message extends Data | Response> (
+  /** @inheritdoc */
+  public registerWorkerMessageListener<Message extends Data | Response> (
     messageChannel: ThreadWorkerWithMessageChannel,
     listener: (message: MessageValue<Message>) => void
   ): void {
     messageChannel.port2?.on('message', listener)
   }
 
-  protected unregisterWorkerMessageListener<Message extends Data | Response> (
-    messageChannel: ThreadWorkerWithMessageChannel,
-    listener: (message: MessageValue<Message>) => void
-  ): void {
-    messageChannel.port2?.removeListener('message', listener)
-  }
-
   protected createWorker (): ThreadWorkerWithMessageChannel {
     return new Worker(this.filePath, {
       env: SHARE_ENV
@@ -82,8 +78,7 @@ export class FixedThreadPool<
     worker.postMessage({ parent: port1 }, [port1])
     worker.port1 = port1
     worker.port2 = port2
-    // we will attach a listener for every task,
-    // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
-    worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
+    // Listen worker messages.
+    this.registerWorkerMessageListener(worker, super.workerListener())
   }
 }