fix: fix possible null exception with worker_threads pools
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Dec 2023 10:27:30 +0000 (11:27 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Dec 2023 10:27:30 +0000 (11:27 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
CHANGELOG.md
src/pools/thread/fixed.ts

index ddfd15e093aa759830b95f773f5434a4ac52154f..3ba761e395bc0468d02cdbb216f378c332f2ec16 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Fix possible null exception with worker_threads pools.
+
 ## [3.0.13] - 2023-12-12
 
 ### Fixed
index 73740baa92110e213b427fe0ca1f8aeafebe307c..28f8fceba679e5f9053820b8436583caf69f20af 100644 (file)
@@ -81,9 +81,7 @@ export class FixedThreadPool<
     message: MessageValue<Data>,
     transferList?: TransferListItem[]
   ): void {
-    (
-      this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel
-    )?.port1?.postMessage(
+    this.workerNodes[workerNodeKey].messageChannel?.port1?.postMessage(
       { ...message, workerId: this.getWorkerInfo(workerNodeKey).id },
       transferList
     )
@@ -109,9 +107,10 @@ export class FixedThreadPool<
     workerNodeKey: number,
     listener: (message: MessageValue<Message>) => void
   ): void {
-    (
-      this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel
-    ).port1.on('message', listener)
+    this.workerNodes[workerNodeKey].messageChannel?.port1?.on(
+      'message',
+      listener
+    )
   }
 
   /** @inheritDoc */
@@ -119,9 +118,10 @@ export class FixedThreadPool<
     workerNodeKey: number,
     listener: (message: MessageValue<Message>) => void
   ): void {
-    (
-      this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel
-    ).port1.once('message', listener)
+    this.workerNodes[workerNodeKey].messageChannel?.port1?.once(
+      'message',
+      listener
+    )
   }
 
   /** @inheritDoc */
@@ -129,9 +129,10 @@ export class FixedThreadPool<
     workerNodeKey: number,
     listener: (message: MessageValue<Message>) => void
   ): void {
-    (
-      this.workerNodes[workerNodeKey]?.messageChannel as MessageChannel
-    ).port1.off('message', listener)
+    this.workerNodes[workerNodeKey].messageChannel?.port1?.off(
+      'message',
+      listener
+    )
   }
 
   /** @inheritDoc */