- protected messageListener (message: MessageValue<Data, Data>): void {
- if (message.workerId === this.id) {
- if (message.ready != null) {
- // Startup message received
- this.sendReadyResponse()
- } else 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.id != null && message.data != null) {
- // Task message received
- this.run(message)
- } else if (message.kill === true) {
- // Kill message received
- this.stopCheckActive()
+ protected messageListener (message: MessageValue<Data>): void {
+ 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
+ }
+ this.sendToMainWorker({
+ taskFunctionOperation,
+ taskFunctionOperationStatus: response.status,
+ taskFunctionName,
+ ...(!response.status &&
+ response?.error != null && {
+ workerError: {
+ name: taskFunctionName as string,
+ 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' })
+ })
+ .finally(() => {
+ this.emitDestroy()
+ })
+ .catch(EMPTY_FUNCTION)
+ } else {
+ try {
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
+ this.opts.killHandler?.() as void
+ this.sendToMainWorker({ kill: 'success' })
+ } catch {
+ this.sendToMainWorker({ kill: 'failure' })
+ } finally {