+ this.taskFunctions.set(name, boundFn)
+ this.sendTaskFunctionsListToMainWorker()
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ /**
+ * Removes a task function from the worker.
+ *
+ * @param name - The name of the task function to remove.
+ * @returns Whether the task function existed and was removed or not.
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty 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 the task function used as default task function.
+ */
+ public removeTaskFunction (name: string): boolean {
+ this.checkTaskFunctionName(name)
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot remove the task function with the default reserved name'
+ )
+ }
+ if (
+ this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ throw new Error(
+ 'Cannot remove the task function used as the default task function'
+ )
+ }
+ const deleteStatus = this.taskFunctions.delete(name)
+ this.sendTaskFunctionsListToMainWorker()
+ return deleteStatus
+ }
+
+ /**
+ * Lists the names of the worker's task functions.
+ *
+ * @returns The names of the worker's task functions.
+ */
+ public listTaskFunctions (): string[] {
+ const names: string[] = [...this.taskFunctions.keys()]
+ let defaultTaskFunctionName: string = DEFAULT_TASK_NAME
+ for (const [name, fn] of this.taskFunctions) {
+ if (
+ name !== DEFAULT_TASK_NAME &&
+ fn === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ defaultTaskFunctionName = name
+ break
+ }
+ }
+ return [
+ names[names.indexOf(DEFAULT_TASK_NAME)],
+ defaultTaskFunctionName,
+ ...names.filter(
+ name => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName
+ )
+ ]
+ }
+
+ /**
+ * Sets the default task function to use in the worker.
+ *
+ * @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 or an empty 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 {
+ this.checkTaskFunctionName(name)
+ 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) as TaskFunction<Data, Response>
+ )
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ private checkTaskFunctionName (name: string): void {
+ if (typeof name !== 'string') {
+ throw new TypeError('name parameter is not a string')
+ }
+ if (typeof name === 'string' && name.trim().length === 0) {
+ throw new TypeError('name parameter is an empty string')
+ }
+ }
+
+ /**
+ * Handles the ready message sent by the main worker.
+ *
+ * @param message - The ready message.
+ */
+ protected abstract handleReadyMessage (message: MessageValue<Data>): void
+
+ /**
+ * Worker message listener.
+ *
+ * @param message - The received message.
+ */
+ protected messageListener (message: MessageValue<Data>): void {
+ this.checkMessageWorkerId(message)
+ if (message.statistics != null) {