- protected runSync (
- fn: WorkerSyncFunction<Data, Response>,
- message: MessageValue<Data>
- ): void {
+ protected readonly run = (task: Task<Data>): void => {
+ const { name, taskId, data } = task
+ const taskFunctionName = name ?? DEFAULT_TASK_NAME
+ if (!this.taskFunctions.has(taskFunctionName)) {
+ this.sendToMainWorker({
+ workerError: {
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ name: name!,
+ message: `Task function '${name}' not found`,
+ data
+ },
+ taskId
+ })
+ return
+ }
+ const fn = this.taskFunctions.get(taskFunctionName)
+ if (isAsyncFunction(fn)) {
+ this.runAsync(fn as TaskAsyncFunction<Data, Response>, task)
+ } else {
+ this.runSync(fn as TaskSyncFunction<Data, Response>, task)
+ }
+ }
+
+ /**
+ * Runs the given task function synchronously.
+ *
+ * @param fn - Task function that will be executed.
+ * @param task - Input data for the task function.
+ */
+ protected readonly runSync = (
+ fn: TaskSyncFunction<Data, Response>,
+ task: Task<Data>
+ ): void => {
+ const { name, taskId, data } = task