From ef41a6e6f04e66c9732334e673758b2f4e4b0730 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 15 Apr 2023 18:56:40 +0200 Subject: [PATCH] fix: fix TS type definitions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 5 +++++ src/pools/abstract-pool.ts | 12 ++++++++---- src/pools/pool.ts | 8 ++++---- src/pools/thread/fixed.ts | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18ae3e60..ab66e3fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 7d8531aa..ffbda872 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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 { + public async execute (data?: Data): Promise { const [workerNodeKey, workerNode] = this.chooseWorkerNode() const submittedTask: Task = { // 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) + } + } } diff --git a/src/pools/pool.ts b/src/pools/pool.ts index f4a17915..d540bbd2 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -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 + execute: (data?: Data) => Promise /** * Shutdowns every current worker in this pool. */ diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 3dc10821..7753d1d6 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -68,10 +68,10 @@ export class FixedThreadPool< /** @inheritDoc */ protected registerWorkerMessageListener( - messageChannel: ThreadWorkerWithMessageChannel, + worker: ThreadWorkerWithMessageChannel, listener: (message: MessageValue) => void ): void { - messageChannel.port2?.on('message', listener) + worker.port2?.on('message', listener) } /** @inheritDoc */ -- 2.34.1