+ if (this.isMain == null) {
+ throw new Error('isMain parameter is mandatory')
+ }
+ this.checkTaskFunctions(taskFunctions)
+ this.checkWorkerOptions(this.opts)
+ if (!this.isMain) {
+ // Should be once() but Node.js on windows has a bug that prevents it from working
+ this.getMainWorker().on('message', this.handleReadyMessage.bind(this))
+ }
+ }
+
+ private checkWorkerOptions (opts: WorkerOptions): void {
+ checkValidWorkerOptions(opts)
+ this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
+ }
+
+ /**
+ * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
+ *
+ * @param taskFunctions - The task function(s) parameter that should be checked.
+ */
+ private checkTaskFunctions (
+ taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>
+ ): void {
+ if (taskFunctions == null) {
+ throw new Error('taskFunctions parameter is mandatory')
+ }
+ this.taskFunctions = new Map<string, TaskFunction<Data, Response>>()
+ if (typeof taskFunctions === 'function') {
+ const boundFn = taskFunctions.bind(this)
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ this.taskFunctions.set(
+ typeof taskFunctions.name === 'string' &&
+ taskFunctions.name.trim().length > 0
+ ? taskFunctions.name
+ : 'fn1',
+ boundFn
+ )
+ } else if (isPlainObject(taskFunctions)) {
+ let firstEntry = true
+ for (const [name, fn] of Object.entries(taskFunctions)) {
+ checkValidTaskFunctionEntry<Data, Response>(name, fn)
+ const boundFn = fn.bind(this)
+ if (firstEntry) {
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ firstEntry = false
+ }
+ this.taskFunctions.set(name, boundFn)
+ }
+ if (firstEntry) {
+ throw new Error('taskFunctions parameter object is empty')
+ }
+ } else {
+ throw new TypeError(
+ 'taskFunctions parameter is not a function or a plain object'
+ )
+ }
+ }
+
+ /**
+ * Checks if the worker has a task function with the given name.
+ *
+ * @param name - The name of the task function to check.
+ * @returns Whether the worker has a task function with the given name or not.
+ */
+ public hasTaskFunction (name: string): TaskFunctionOperationResult {
+ try {
+ checkTaskFunctionName(name)
+ } catch (error) {
+ return { status: false, error: error as Error }
+ }
+ return { status: this.taskFunctions.has(name) }
+ }
+
+ /**
+ * Adds a task function to the worker.
+ * If a task function with the same name already exists, it is replaced.
+ *
+ * @param name - The name of the task function to add.
+ * @param fn - The task function to add.
+ * @returns Whether the task function was added or not.
+ */
+ public addTaskFunction (
+ name: string,
+ fn: TaskFunction<Data, Response>
+ ): TaskFunctionOperationResult {
+ try {
+ checkTaskFunctionName(name)
+ if (name === DEFAULT_TASK_NAME) {
+ throw new Error(
+ 'Cannot add a task function with the default reserved name'
+ )
+ }
+ if (typeof fn !== 'function') {
+ throw new TypeError('fn parameter is not a function')
+ }
+ const boundFn = fn.bind(this)
+ if (
+ this.taskFunctions.get(name) ===
+ this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ) {
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ }
+ this.taskFunctions.set(name, boundFn)
+ this.sendTaskFunctionNamesToMainWorker()
+ 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.sendTaskFunctionNamesToMainWorker()
+ return { status: deleteStatus }
+ } catch (error) {
+ return { status: false, error: error as Error }
+ }
+ }
+
+ /**
+ * Lists the names of the worker's task functions.
+ *
+ * @returns The names of the worker's task functions.
+ */
+ public listTaskFunctionNames (): 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.
+ */
+ 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'
+ )
+ }
+ this.taskFunctions.set(
+ DEFAULT_TASK_NAME,
+ this.taskFunctions.get(name) as TaskFunction<Data, Response>