+ this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ throw new Error(
+ 'Cannot remove the task function used as the default task function'
+ )
+ }
+ return this.taskFunctions.delete(name)
+ }
+
+ /**
+ * Sets the default task function to use when no task function name is specified.
+ *
+ * @param name - The name of the task function to use as default task function.
+ * @returns Whether the default task function was set or not.
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
+ */
+ public setDefaultTaskFunction (name: string): boolean {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot set the default task function reserved name as the default task function'
+ )
+ }
+ if (!this.taskFunctions.has(name)) {
+ throw new Error(
+ 'Cannot set the default task function to a non-existing task function'
+ )
+ }
+ try {
+ this.taskFunctions.set(
+ DEFAULT_TASK_NAME,
+ this.taskFunctions.get(name)?.bind(this) as WorkerFunction<
+ Data,
+ Response
+ >
+ )
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ /**
+ * Worker message listener.
+ *
+ * @param message - Message received.
+ */
+ protected messageListener (message: MessageValue<Data, Data>): void {
+ if (message.workerId === this.id) {
+ if (message.ready != null) {
+ // Startup message received
+ this.workerReady()
+ } else if (message.statistics != null) {
+ // Statistics message received
+ this.statistics = message.statistics
+ } else if (message.checkAlive != null) {
+ // Check alive message received
+ message.checkAlive ? this.startCheckAlive() : this.stopCheckAlive()
+ } else if (message.id != null && message.data != null) {
+ // Task message received
+ this.run(message)
+ } else if (message.kill === true) {
+ // Kill message received
+ this.stopCheckAlive()
+ this.emitDestroy()
+ }
+ }
+ }
+
+ /**
+ * Notifies the main worker that this worker is ready to process tasks.
+ */
+ protected workerReady (): void {
+ !this.isMain && this.sendToMainWorker({ ready: true, workerId: this.id })
+ }
+
+ /**
+ * Starts the worker alive check interval.
+ */
+ private startCheckAlive (): void {
+ this.lastTaskTimestamp = performance.now()
+ this.aliveInterval = setInterval(
+ this.checkAlive.bind(this),
+ (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
+ )
+ this.checkAlive.bind(this)()
+ }
+
+ /**
+ * Stops the worker alive check interval.
+ */
+ private stopCheckAlive (): void {
+ this.aliveInterval != null && clearInterval(this.aliveInterval)
+ }
+
+ /**
+ * Checks if the worker should be terminated, because its living too long.
+ */
+ private checkAlive (): void {
+ if (
+ performance.now() - this.lastTaskTimestamp >