+ 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 null
+ })
+ .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 {
+ this.emitDestroy()
+ }
+ }
+ }
+
+ /**
+ * Check if the message worker id is set and matches the worker id.
+ *
+ * @param message - The message to check.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
+ */
+ private checkMessageWorkerId (message: MessageValue<Data>): void {
+ if (message.workerId == null) {
+ throw new Error('Message worker id is not set')
+ } else if (message.workerId != null && message.workerId !== this.id) {
+ throw new Error(
+ `Message worker id ${message.workerId} does not match the worker id ${this.id}`
+ )
+ }
+ }
+
+ /**
+ * Starts the worker check active interval.
+ */
+ private startCheckActive (): void {
+ this.lastTaskTimestamp = performance.now()
+ this.activeInterval = setInterval(
+ this.checkActive.bind(this),
+ (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
+ )
+ }
+
+ /**
+ * Stops the worker check active interval.
+ */
+ private stopCheckActive (): void {
+ if (this.activeInterval != null) {
+ clearInterval(this.activeInterval)
+ delete this.activeInterval
+ }
+ }
+
+ /**
+ * Checks if the worker should be terminated, because its living too long.
+ */
+ private checkActive (): void {
+ if (
+ performance.now() - this.lastTaskTimestamp >
+ (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
+ ) {
+ this.sendToMainWorker({ kill: this.opts.killBehavior })