fix: fix TS type definitions
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 16:56:40 +0000 (18:56 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 16:56:40 +0000 (18:56 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
src/pools/pool.ts
src/pools/thread/fixed.ts

index 18ae3e604e8cb4e530541d0115659484d64e9c87..ab66e3fb81560e5a68e547bc4d662d1a64a7f2ec 100644 (file)
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Fix typescript type definition for worker function: ensure the input data is optional.
+- Fix typescript type definition for pool execute(): ensure the input data is optional.
+
 ## [2.4.9] - 2023-04-15
 
 ### Added
index 7d8531aa8d5d5480f8395f0263c90248f955358d..ffbda872af76d22fd7d037b39dd54025568e39c5 100644 (file)
@@ -256,9 +256,7 @@ export abstract class AbstractPool<
     tasksQueueOptions?: TasksQueueOptions
   ): void {
     if (this.opts.enableTasksQueue === true && !enable) {
-      for (const [workerNodeKey] of this.workerNodes.entries()) {
-        this.flushTasksQueue(workerNodeKey)
-      }
+      this.flushTasksQueues()
     }
     this.opts.enableTasksQueue = enable
     this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions)
@@ -309,7 +307,7 @@ export abstract class AbstractPool<
   }
 
   /** @inheritDoc */
-  public async execute (data: Data): Promise<Response> {
+  public async execute (data?: Data): Promise<Response> {
     const [workerNodeKey, workerNode] = this.chooseWorkerNode()
     const submittedTask: Task<Data> = {
       // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -656,4 +654,10 @@ export abstract class AbstractPool<
     const workerNodeKey = this.getWorkerNodeKey(worker)
     this.flushTasksQueue(workerNodeKey)
   }
+
+  private flushTasksQueues (): void {
+    for (const [workerNodeKey] of this.workerNodes.entries()) {
+      this.flushTasksQueue(workerNodeKey)
+    }
+  }
 }
index f4a1791504f16964c94f378be5a35b066e0cc0e9..d540bbd267256677ba872fc6b08f7690f155dd5e 100644 (file)
@@ -151,12 +151,12 @@ export interface IPool<
    */
   findFreeWorkerNodeKey: () => number
   /**
-   * Executes the function specified in the constructor with the task data input parameter.
+   * Executes the function specified in the worker constructor with the task data input parameter.
    *
-   * @param data - The task input data for the specified function. This can only be serializable data.
-   * @returns Promise that will be resolved when the task is successfully completed.
+   * @param data - The task input data for the specified worker function. This can only be serializable data.
+   * @returns Promise that will be fulfilled when the task is completed.
    */
-  execute: (data: Data) => Promise<Response>
+  execute: (data?: Data) => Promise<Response>
   /**
    * Shutdowns every current worker in this pool.
    */
index 3dc10821e2cdb9198e3eea62e287a4fc4d8aa425..7753d1d60cb21910a5f871338889023ef4de12c1 100644 (file)
@@ -68,10 +68,10 @@ export class FixedThreadPool<
 
   /** @inheritDoc */
   protected registerWorkerMessageListener<Message extends Data | Response>(
-    messageChannel: ThreadWorkerWithMessageChannel,
+    worker: ThreadWorkerWithMessageChannel,
     listener: (message: MessageValue<Message>) => void
   ): void {
-    messageChannel.port2?.on('message', listener)
+    worker.port2?.on('message', listener)
   }
 
   /** @inheritDoc */