Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-worker_thr...
[poolifier.git] / src / worker / thread-worker.ts
index dc628bed08fa4b23f29724f88f41484b0097afef..d6a3699220b63fad5dc3191c1b4c1ca72d112bc8 100644 (file)
@@ -7,7 +7,7 @@ import {
 import type { MessageValue } from '../utility-types'
 import { AbstractWorker } from './abstract-worker'
 import type { WorkerOptions } from './worker-options'
-import type { TaskFunctions, WorkerFunction } from './worker-functions'
+import type { TaskFunction, TaskFunctions } from './task-functions'
 
 /**
  * A thread worker used by a poolifier `ThreadPool`.
@@ -38,9 +38,7 @@ export class ThreadWorker<
    * @param opts - Options for the worker.
    */
   public constructor (
-    taskFunctions:
-    | WorkerFunction<Data, Response>
-    | TaskFunctions<Data, Response>,
+    taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     opts: WorkerOptions = {}
   ) {
     super(
@@ -55,14 +53,25 @@ export class ThreadWorker<
   /** @inheritDoc */
   protected handleReadyMessage (message: MessageValue<Data>): void {
     if (
-      !this.isMain &&
       message.workerId === this.id &&
-      message.ready != null &&
+      message.ready === false &&
       message.port != null
     ) {
-      this.port = message.port
-      this.port.on('message', this.messageListener.bind(this))
-      this.sendToMainWorker({ ready: true, workerId: this.id })
+      try {
+        this.port = message.port
+        this.port.on('message', this.messageListener.bind(this))
+        this.sendToMainWorker({
+          ready: true,
+          taskFunctions: this.listTaskFunctions(),
+          workerId: this.id
+        })
+      } catch {
+        this.sendToMainWorker({
+          ready: false,
+          taskFunctions: this.listTaskFunctions(),
+          workerId: this.id
+        })
+      }
     }
   }
 
@@ -84,7 +93,7 @@ export class ThreadWorker<
   }
 
   /** @inheritDoc */
-  protected handleError (e: Error | string): string {
-    return e as string
+  protected handleError (error: Error | string): string {
+    return error as string
   }
 }