X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=dc1f6adc02ba8a7b433f08ae8e4d3f4d9abe7b3d;hb=4e38fd2175c62f93f766733a1feff14a4a9e9958;hp=bc62ade55ef15efac79c8c9e8835a45cb697aa40;hpb=619e0f0417560f44a99f4508a8f314db54173c0c;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index bc62ade5..dc1f6adc 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -18,7 +18,7 @@ import { KillBehaviors, type WorkerOptions } from './worker-options' import type { TaskAsyncFunction, TaskFunction, - TaskFunctionOperationReturnType, + TaskFunctionOperationResult, TaskFunctions, TaskSyncFunction } from './task-functions' @@ -201,7 +201,7 @@ export abstract class AbstractWorker< * @param name - The name of the task function to check. * @returns Whether the worker has a task function with the given name or not. */ - public hasTaskFunction (name: string): TaskFunctionOperationReturnType { + public hasTaskFunction (name: string): TaskFunctionOperationResult { try { this.checkTaskFunctionName(name) } catch (error) { @@ -221,7 +221,7 @@ export abstract class AbstractWorker< public addTaskFunction ( name: string, fn: TaskFunction - ): TaskFunctionOperationReturnType { + ): TaskFunctionOperationResult { try { this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { @@ -253,7 +253,7 @@ export abstract class AbstractWorker< * @param name - The name of the task function to remove. * @returns Whether the task function existed and was removed or not. */ - public removeTaskFunction (name: string): TaskFunctionOperationReturnType { + public removeTaskFunction (name: string): TaskFunctionOperationResult { try { this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { @@ -309,7 +309,7 @@ export abstract class AbstractWorker< * @param name - The name of the task function to use as default task function. * @returns Whether the default task function was set or not. */ - public setDefaultTaskFunction (name: string): TaskFunctionOperationReturnType { + public setDefaultTaskFunction (name: string): TaskFunctionOperationResult { try { this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { @@ -326,6 +326,7 @@ export abstract class AbstractWorker< DEFAULT_TASK_NAME, this.taskFunctions.get(name) as TaskFunction ) + this.sendTaskFunctionNamesToMainWorker() return { status: true } } catch (error) { return { status: false, error: error as Error } @@ -376,8 +377,8 @@ export abstract class AbstractWorker< protected handleTaskFunctionOperationMessage ( message: MessageValue ): void { - const { taskFunctionOperation, taskFunction, taskFunctionName } = message - let response!: TaskFunctionOperationReturnType + const { taskFunctionOperation, taskFunctionName, taskFunction } = message + let response!: TaskFunctionOperationResult if (taskFunctionOperation === 'add') { response = this.addTaskFunction( taskFunctionName as string, @@ -395,10 +396,14 @@ export abstract class AbstractWorker< this.sendToMainWorker({ taskFunctionOperation, taskFunctionOperationStatus: response.status, - workerError: { - name: taskFunctionName as string, - message: this.handleError(response.error as Error | string) - } + taskFunctionName, + ...(!response.status && + response?.error != null && { + workerError: { + name: taskFunctionName as string, + message: this.handleError(response.error as Error | string) + } + }) }) } @@ -606,7 +611,7 @@ export abstract class AbstractWorker< taskPerformance, taskId }) - return null + return undefined }) .catch(error => { this.sendToMainWorker({