- 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.id != null && message.data != null) {
- // Task message received
- this.run(message)
- } else if (message.kill === true) {
- // Kill message received
- !this.isMain && this.stopCheckActive()
- this.emitDestroy()
+ this.checkMessageWorkerId(message)
+ const {
+ statistics,
+ checkActive,
+ taskFunctionOperation,
+ taskId,
+ data,
+ kill,
+ } = message
+ if (statistics != null) {
+ // Statistics message received
+ this.statistics = statistics
+ } else if (checkActive != null) {
+ // Check active message received
+ checkActive ? this.startCheckActive() : this.stopCheckActive()
+ } else if (taskFunctionOperation != null) {
+ // Task function operation message received
+ this.handleTaskFunctionOperationMessage(message)
+ } else if (taskId != null && data != null) {
+ // Task message received
+ this.run(message)
+ } else if (kill === true) {
+ // Kill message received
+ this.handleKillMessage(message)
+ }
+ }
+
+ 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),
+ },
+ }),
+ })
+ }
+
+ /**
+ * Handles a kill message sent by the main worker.
+ * @param message - The kill message.
+ */
+ protected handleKillMessage (message: MessageValue<Data>): void {
+ this.stopCheckActive()
+ if (isAsyncFunction(this.opts.killHandler)) {
+ ;(this.opts.killHandler() as Promise<void>)
+ .then(() => {
+ this.sendToMainWorker({ kill: 'success' })
+ return undefined
+ })
+ .catch(() => {
+ this.sendToMainWorker({ kill: 'failure' })
+ })
+ } else {
+ try {
+ ;(this.opts.killHandler as (() => void) | undefined)?.()
+ this.sendToMainWorker({ kill: 'success' })
+ } catch {
+ this.sendToMainWorker({ kill: 'failure' })