build(deps): bump poolifier in /examples/typescript/smtp-client-pool
[poolifier.git] / src / worker / thread-worker.ts
index d5ce3a2f130da3efbe81f0aa0e3b3e7394033822..7f3cd950a992269fb7917b7eda296419a501b6f8 100644 (file)
@@ -4,10 +4,10 @@ import {
   parentPort,
   threadId
 } from 'node:worker_threads'
-import type { MessageValue } from '../utility-types'
-import { AbstractWorker } from './abstract-worker'
-import type { WorkerOptions } from './worker-options'
-import type { TaskFunction, TaskFunctions } from './task-functions'
+import type { MessageValue } from '../utility-types.js'
+import { AbstractWorker } from './abstract-worker.js'
+import type { WorkerOptions } from './worker-options.js'
+import type { TaskFunction, TaskFunctions } from './task-functions.js'
 
 /**
  * A thread worker used by a poolifier `ThreadPool`.
@@ -30,7 +30,8 @@ export class ThreadWorker<
   /**
    * Message port used to communicate with the main worker.
    */
-  private port!: MessagePort
+  private port?: MessagePort
+
   /**
    * Constructs a new poolifier thread worker.
    *
@@ -41,13 +42,8 @@ export class ThreadWorker<
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     opts: WorkerOptions = {}
   ) {
-    super(
-      'poolifier:thread-worker',
-      isMainThread,
-      parentPort as MessagePort,
-      taskFunctions,
-      opts
-    )
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    super(isMainThread, parentPort!, taskFunctions, opts)
   }
 
   /** @inheritDoc */
@@ -86,8 +82,10 @@ export class ThreadWorker<
   }
 
   /** @inheritDoc */
-  protected sendToMainWorker (message: MessageValue<Response>): void {
-    this.port.postMessage({ ...message, workerId: this.id })
+  protected readonly sendToMainWorker = (
+    message: MessageValue<Response>
+  ): void => {
+    this.port?.postMessage({ ...message, workerId: this.id })
   }
 
   /**