fix: fix possible null exception with worker_threads pools
[poolifier.git] / src / worker / thread-worker.ts
index fdbab44ed5f1356e6502a814f56e3a94ef54215d..e133430f5029813aaac582c1a7156f053293d75f 100644 (file)
@@ -30,7 +30,7 @@ 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,7 +41,7 @@ export class ThreadWorker<
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     opts: WorkerOptions = {}
   ) {
-    super(isMainThread, parentPort as MessagePort, taskFunctions)
+    super(isMainThread, parentPort as MessagePort, taskFunctions, opts)
   }
 
   /** @inheritDoc */
@@ -80,8 +80,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 })
   }
 
   /**