chore: use `biome` instead of `rome`
[poolifier.git] / src / worker / abstract-worker.ts
index 033ed2a5e663fbcd7c88b4317adc240e20b2d298..a0d78dbd08f2023e38cd37dbff6dbc74064168fb 100644 (file)
@@ -103,6 +103,27 @@ export abstract class AbstractWorker<
     delete this.opts.async
   }
 
+  private checkValidTaskFunction (
+    name: string,
+    fn: TaskFunction<Data, Response>
+  ): void {
+    if (typeof name !== 'string') {
+      throw new TypeError(
+        'A taskFunctions parameter object key is not a string'
+      )
+    }
+    if (typeof name === 'string' && name.trim().length === 0) {
+      throw new TypeError(
+        'A taskFunctions parameter object key is an empty string'
+      )
+    }
+    if (typeof fn !== 'function') {
+      throw new TypeError(
+        'A taskFunctions parameter object value is not a function'
+      )
+    }
+  }
+
   /**
    * Checks if the `taskFunctions` parameter is passed to the constructor.
    *
@@ -128,21 +149,7 @@ export abstract class AbstractWorker<
     } else if (isPlainObject(taskFunctions)) {
       let firstEntry = true
       for (const [name, fn] of Object.entries(taskFunctions)) {
-        if (typeof name !== 'string') {
-          throw new TypeError(
-            'A taskFunctions parameter object key is not a string'
-          )
-        }
-        if (typeof name === 'string' && name.trim().length === 0) {
-          throw new TypeError(
-            'A taskFunctions parameter object key an empty string'
-          )
-        }
-        if (typeof fn !== 'function') {
-          throw new TypeError(
-            'A taskFunctions parameter object value is not a function'
-          )
-        }
+        this.checkValidTaskFunction(name, fn)
         const boundFn = fn.bind(this)
         if (firstEntry) {
           this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
@@ -344,7 +351,7 @@ export abstract class AbstractWorker<
   protected handleKillMessage (message: MessageValue<Data>): void {
     this.stopCheckActive()
     if (isAsyncFunction(this.opts.killHandler)) {
-      (this.opts.killHandler?.() as Promise<void>)
+      ;(this.opts.killHandler?.() as Promise<void>)
         .then(() => {
           this.sendToMainWorker({ kill: 'success', workerId: this.id })
           return null
@@ -422,6 +429,7 @@ export abstract class AbstractWorker<
    * Returns the main worker.
    *
    * @returns Reference to the main worker.
+   * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
    */
   protected getMainWorker (): MainWorker {
     if (this.mainWorker == null) {