- if (message.workerId != null && message.workerId !== this.id) {
- throw new Error('Message worker id does not match worker id')
- } else if (message.workerId === this.id) {
- if (message.statistics != null) {
- // Statistics message received
- this.statistics = message.statistics
- } else if (message.checkActive != null) {
- // Check active message received
- !this.isMain && message.checkActive
- ? this.startCheckActive()
- : this.stopCheckActive()
- } else if (message.taskId != null && message.data != null) {
- // Task message received
- this.run(message)
- } else if (message.kill === true) {
- // Kill message received
- this.handleKillMessage(message)
- }
+ this.checkMessageWorkerId(message)
+ if (message.statistics != null) {
+ // Statistics message received
+ this.statistics = message.statistics
+ } else if (message.checkActive != null) {
+ // Check active message received
+ message.checkActive ? this.startCheckActive() : this.stopCheckActive()
+ } else if (message.taskFunctionOperation != null) {
+ // Task function operation message received
+ this.handleTaskFunctionOperationMessage(message)
+ } else if (message.taskId != null && message.data != null) {
+ // Task message received
+ this.run(message)
+ } else if (message.kill === true) {
+ // Kill message received
+ this.handleKillMessage(message)
+ }
+ }
+
+ protected handleTaskFunctionOperationMessage (
+ message: MessageValue<Data>
+ ): void {
+ const { taskFunctionOperation, taskFunctionName, taskFunction } = message
+ let response!: TaskFunctionOperationResult
+ switch (taskFunctionOperation) {
+ case 'add':
+ response = this.addTaskFunction(
+ taskFunctionName as string,
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
+ new Function(`return ${taskFunction as string}`)() as TaskFunction<
+ Data,
+ Response
+ >
+ )
+ break
+ case 'remove':
+ response = this.removeTaskFunction(taskFunctionName as string)
+ break
+ case 'default':
+ response = this.setDefaultTaskFunction(taskFunctionName as string)
+ break
+ default:
+ response = { status: false, error: new Error('Unknown task operation') }
+ break