chore: v2.5.3
[poolifier.git] / src / worker / abstract-worker.ts
index 12fb67af32934383a9498e541fadc031e85cbafe..91a45e94b0f36c568326460891332d61d7de0731 100644 (file)
@@ -9,8 +9,11 @@ import type {
   WorkerSyncFunction
 } from '../utility-types'
 import { EMPTY_FUNCTION, isPlainObject } from '../utils'
-import type { KillBehavior, WorkerOptions } from './worker-options'
-import { KillBehaviors } from './worker-options'
+import {
+  type KillBehavior,
+  KillBehaviors,
+  type WorkerOptions
+} from './worker-options'
 
 const DEFAULT_FUNCTION_NAME = 'default'
 const DEFAULT_MAX_INACTIVE_TIME = 60000
@@ -87,7 +90,7 @@ export abstract class AbstractWorker<
     this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
     this.opts.maxInactiveTime =
       opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
-    this.opts.async = opts.async ?? false
+    delete this.opts.async
   }
 
   /**
@@ -103,14 +106,6 @@ export abstract class AbstractWorker<
     if (taskFunctions == null) {
       throw new Error('taskFunctions parameter is mandatory')
     }
-    if (
-      typeof taskFunctions !== 'function' &&
-      typeof taskFunctions !== 'object'
-    ) {
-      throw new TypeError(
-        'taskFunctions parameter is not a function or an object'
-      )
-    }
     this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
     if (typeof taskFunctions === 'function') {
       this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
@@ -132,7 +127,9 @@ export abstract class AbstractWorker<
         throw new Error('taskFunctions parameter object is empty')
       }
     } else {
-      throw new TypeError('taskFunctions parameter is not an object literal')
+      throw new TypeError(
+        'taskFunctions parameter is not a function or a plain object'
+      )
     }
   }
 
@@ -213,16 +210,22 @@ export abstract class AbstractWorker<
   ): void {
     try {
       const startTimestamp = performance.now()
+      const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
       const res = fn(message.data)
       const runTime = performance.now() - startTimestamp
       this.sendToMainWorker({
         data: res,
-        id: message.id,
-        runTime
+        runTime,
+        waitTime,
+        id: message.id
       })
     } catch (e) {
       const err = this.handleError(e as Error)
-      this.sendToMainWorker({ error: err, id: message.id })
+      this.sendToMainWorker({
+        error: err,
+        errorData: message.data,
+        id: message.id
+      })
     } finally {
       !this.isMain && (this.lastTaskTimestamp = performance.now())
     }
@@ -239,19 +242,25 @@ export abstract class AbstractWorker<
     message: MessageValue<Data>
   ): void {
     const startTimestamp = performance.now()
+    const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
     fn(message.data)
       .then(res => {
         const runTime = performance.now() - startTimestamp
         this.sendToMainWorker({
           data: res,
-          id: message.id,
-          runTime
+          runTime,
+          waitTime,
+          id: message.id
         })
         return null
       })
       .catch(e => {
         const err = this.handleError(e as Error)
-        this.sendToMainWorker({ error: err, id: message.id })
+        this.sendToMainWorker({
+          error: err,
+          errorData: message.data,
+          id: message.id
+        })
       })
       .finally(() => {
         !this.isMain && (this.lastTaskTimestamp = performance.now())
@@ -268,7 +277,7 @@ export abstract class AbstractWorker<
     name = name ?? DEFAULT_FUNCTION_NAME
     const fn = this.taskFunctions.get(name)
     if (fn == null) {
-      throw new Error(`Task function "${name}" not found`)
+      throw new Error(`Task function '${name}' not found`)
     }
     return fn
   }