+ protected handleTaskFunctionOperationMessage (
+ message: MessageValue<Data>
+ ): void {
+ const { taskFunctionOperation, taskFunctionProperties, taskFunction } =
+ message
+ if (taskFunctionProperties == null) {
+ throw new Error(
+ 'Cannot handle task function operation message without task function properties'
+ )
+ }
+ let response: TaskFunctionOperationResult
+ switch (taskFunctionOperation) {
+ case 'add':
+ response = this.addTaskFunction(taskFunctionProperties.name, {
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
+ taskFunction: new Function(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ `return ${taskFunction!}`
+ )() as TaskFunction<Data, Response>,
+ ...(taskFunctionProperties.priority != null && {
+ priority: taskFunctionProperties.priority,
+ }),
+ ...(taskFunctionProperties.strategy != null && {
+ strategy: taskFunctionProperties.strategy,
+ }),
+ })
+ break
+ case 'remove':
+ response = this.removeTaskFunction(taskFunctionProperties.name)
+ break
+ case 'default':
+ response = this.setDefaultTaskFunction(taskFunctionProperties.name)
+ break
+ default:
+ response = { status: false, error: new Error('Unknown task operation') }
+ break
+ }
+ this.sendToMainWorker({
+ taskFunctionOperation,
+ taskFunctionOperationStatus: response.status,
+ taskFunctionProperties,
+ ...(!response.status &&
+ response.error != null && {
+ workerError: {
+ name: taskFunctionProperties.name,
+ message: this.handleError(response.error as Error | string),
+ },
+ }),
+ })
+ }
+