+ checkValidTaskFunctionObjectEntry<Data, Response>(name, fn)
+ fn.taskFunction = fn.taskFunction.bind(this)
+ if (
+ this.taskFunctions.get(name) ===
+ this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fn)
+ }
+ this.taskFunctions.set(name, fn)
+ this.sendTaskFunctionsPropertiesToMainWorker()
+ return { status: true }
+ } catch (error) {
+ return { status: false, error: error as Error }
+ }
+ }
+
+ /**
+ * 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.
+ */
+ public removeTaskFunction (name: string): TaskFunctionOperationResult {
+ try {
+ 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.sendTaskFunctionsPropertiesToMainWorker()
+ return { status: deleteStatus }
+ } catch (error) {
+ return { status: false, error: error as Error }
+ }
+ }
+
+ /**
+ * Lists the properties of the worker's task functions.
+ * @returns The properties of the worker's task functions.
+ */
+ public listTaskFunctionsProperties (): TaskFunctionProperties[] {
+ let defaultTaskFunctionName = DEFAULT_TASK_NAME
+ for (const [name, fnObj] of this.taskFunctions) {
+ if (
+ name !== DEFAULT_TASK_NAME &&
+ fnObj === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ defaultTaskFunctionName = name
+ break
+ }
+ }
+ const taskFunctionsProperties: TaskFunctionProperties[] = []
+ for (const [name, fnObj] of this.taskFunctions) {
+ if (name === DEFAULT_TASK_NAME || name === defaultTaskFunctionName) {
+ continue
+ }
+ taskFunctionsProperties.push(buildTaskFunctionProperties(name, fnObj))
+ }
+ return [
+ buildTaskFunctionProperties(
+ DEFAULT_TASK_NAME,
+ this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ),
+ buildTaskFunctionProperties(
+ defaultTaskFunctionName,
+ this.taskFunctions.get(defaultTaskFunctionName)
+ ),
+ ...taskFunctionsProperties,
+ ]
+ }
+
+ /**
+ * 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.
+ */
+ public setDefaultTaskFunction (name: string): TaskFunctionOperationResult {
+ try {
+ 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'
+ )
+ }
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!)
+ this.sendTaskFunctionsPropertiesToMainWorker()
+ return { status: true }
+ } catch (error) {
+ return { status: false, error: error as Error }
+ }
+ }
+
+ /**
+ * 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)
+ const {
+ statistics,
+ checkActive,
+ taskFunctionOperation,
+ taskId,
+ data,
+ kill,
+ } = message
+ if (statistics != null) {