build(deps): apply updates
[poolifier.git] / src / pools / worker-node.ts
index e9680261b591e26707c11c5ca868873f3ca5a6e4..de65f27012634c493d697a0ae9e3972a7f5234db 100644 (file)
@@ -15,11 +15,12 @@ import {
   type IWorkerNode,
   type StrategyData,
   type WorkerInfo,
-  type WorkerNodeEventCallback,
+  type WorkerNodeEventDetail,
   type WorkerType,
   WorkerTypes,
   type WorkerUsage
 } from './worker'
+import { checkWorkerNodeArguments } from './utils'
 
 /**
  * Worker node.
@@ -28,7 +29,8 @@ import {
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
  */
 export class WorkerNode<Worker extends IWorker, Data = unknown>
-implements IWorkerNode<Worker, Data> {
+  extends EventTarget
+  implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public readonly worker: Worker
   /** @inheritdoc */
@@ -41,10 +43,6 @@ implements IWorkerNode<Worker, Data> {
   public messageChannel?: MessageChannel
   /** @inheritdoc */
   public tasksQueueBackPressureSize: number
-  /** @inheritdoc */
-  public onBackPressure?: WorkerNodeEventCallback
-  /** @inheritdoc */
-  public onEmptyQueue?: WorkerNodeEventCallback
   private readonly tasksQueue: Deque<Task<Data>>
   private onBackPressureStarted: boolean
   private onEmptyQueueCount: number
@@ -57,7 +55,8 @@ implements IWorkerNode<Worker, Data> {
    * @param tasksQueueBackPressureSize - The tasks queue back pressure size.
    */
   constructor (worker: Worker, tasksQueueBackPressureSize: number) {
-    this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
+    super()
+    checkWorkerNodeArguments<Worker>(worker, tasksQueueBackPressureSize)
     this.worker = worker
     this.info = this.initWorkerInfo(worker)
     this.usage = this.initWorkerUsage()
@@ -79,13 +78,13 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public enqueueTask (task: Task<Data>): number {
     const tasksQueueSize = this.tasksQueue.push(task)
-    if (
-      this.onBackPressure != null &&
-      this.hasBackPressure() &&
-      !this.onBackPressureStarted
-    ) {
+    if (this.hasBackPressure() && !this.onBackPressureStarted) {
       this.onBackPressureStarted = true
-      this.onBackPressure(this.info.id as number)
+      this.dispatchEvent(
+        new CustomEvent<WorkerNodeEventDetail>('backpressure', {
+          detail: { workerId: this.info.id as number }
+        })
+      )
       this.onBackPressureStarted = false
     }
     return tasksQueueSize
@@ -94,13 +93,13 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public unshiftTask (task: Task<Data>): number {
     const tasksQueueSize = this.tasksQueue.unshift(task)
-    if (
-      this.onBackPressure != null &&
-      this.hasBackPressure() &&
-      !this.onBackPressureStarted
-    ) {
+    if (this.hasBackPressure() && !this.onBackPressureStarted) {
       this.onBackPressureStarted = true
-      this.onBackPressure(this.info.id as number)
+      this.dispatchEvent(
+        new CustomEvent<WorkerNodeEventDetail>('backpressure', {
+          detail: { workerId: this.info.id as number }
+        })
+      )
       this.onBackPressureStarted = false
     }
     return tasksQueueSize
@@ -109,11 +108,7 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public dequeueTask (): Task<Data> | undefined {
     const task = this.tasksQueue.shift()
-    if (
-      this.onEmptyQueue != null &&
-      this.tasksQueue.size === 0 &&
-      this.onEmptyQueueCount === 0
-    ) {
+    if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) {
       this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
     }
     return task
@@ -122,11 +117,7 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public popTask (): Task<Data> | undefined {
     const task = this.tasksQueue.pop()
-    if (
-      this.onEmptyQueue != null &&
-      this.tasksQueue.size === 0 &&
-      this.onEmptyQueueCount === 0
-    ) {
+    if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) {
       this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
     }
     return task
@@ -151,10 +142,10 @@ implements IWorkerNode<Worker, Data> {
   /** @inheritdoc */
   public closeChannel (): void {
     if (this.messageChannel != null) {
-      this.messageChannel?.port1.unref()
-      this.messageChannel?.port2.unref()
-      this.messageChannel?.port1.close()
-      this.messageChannel?.port2.close()
+      this.messageChannel.port1.unref()
+      this.messageChannel.port2.unref()
+      this.messageChannel.port1.close()
+      this.messageChannel.port2.close()
       delete this.messageChannel
     }
   }
@@ -197,7 +188,11 @@ implements IWorkerNode<Worker, Data> {
       return
     }
     ++this.onEmptyQueueCount
-    this.onEmptyQueue?.(this.info.id as number)
+    this.dispatchEvent(
+      new CustomEvent<WorkerNodeEventDetail>('emptyqueue', {
+        detail: { workerId: this.info.id as number }
+      })
+    )
     await sleep(exponentialDelay(this.onEmptyQueueCount))
     await this.startOnEmptyQueue()
   }
@@ -232,17 +227,17 @@ implements IWorkerNode<Worker, Data> {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: new CircularArray<number>()
       },
       waitTime: {
-        history: new CircularArray()
+        history: new CircularArray<number>()
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: new CircularArray<number>()
         },
         active: {
-          history: new CircularArray()
+          history: new CircularArray<number>()
         }
       }
     }
@@ -273,43 +268,19 @@ implements IWorkerNode<Worker, Data> {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: new CircularArray<number>()
       },
       waitTime: {
-        history: new CircularArray()
+        history: new CircularArray<number>()
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: new CircularArray<number>()
         },
         active: {
-          history: new CircularArray()
+          history: new CircularArray<number>()
         }
       }
     }
   }
-
-  private checkWorkerNodeArguments (
-    worker: Worker,
-    tasksQueueBackPressureSize: number
-  ): void {
-    if (worker == null) {
-      throw new TypeError('Cannot construct a worker node without a worker')
-    }
-    if (tasksQueueBackPressureSize == null) {
-      throw new TypeError(
-        'Cannot construct a worker node without a tasks queue back pressure size'
-      )
-    }
-    if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
-      throw new TypeError(
-        'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
-      )
-    }
-    if (tasksQueueBackPressureSize <= 0) {
-      throw new RangeError(
-        'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
-      )
-    }
-  }
 }