fix: avoid duplicate per task function name usage statistics
[poolifier.git] / src / pools / worker.ts
index 58610eaaddacc2beea93809f80675bb8530010c0..5ac90fbf1783e9e04a7e820e37c961e8527385ff 100644 (file)
@@ -1,3 +1,4 @@
+import type { MessageChannel } from 'node:worker_threads'
 import type { CircularArray } from '../circular-array'
 import type { Task } from '../utility-types'
 
@@ -105,8 +106,8 @@ export interface TaskStatistics {
  * Enumeration of worker types.
  */
 export const WorkerTypes = Object.freeze({
-  cluster: 'cluster',
-  thread: 'thread'
+  thread: 'thread',
+  cluster: 'cluster'
 } as const)
 
 /**
@@ -136,6 +137,14 @@ export interface WorkerInfo {
    * Ready flag.
    */
   ready: boolean
+  /**
+   * Task function names.
+   */
+  taskFunctions?: string[]
+  /**
+   * Message channel.
+   */
+  messageChannel?: MessageChannel
 }
 
 /**
@@ -177,9 +186,9 @@ export interface IWorker {
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly on: ((event: 'message', handler: MessageHandler<this>) => void) &
+  readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) &
+  ((event: 'message', handler: MessageHandler<this>) => void) &
   ((event: 'error', handler: ErrorHandler<this>) => void) &
-  ((event: 'online', handler: OnlineHandler<this>) => void) &
   ((event: 'exit', handler: ExitHandler<this>) => void)
   /**
    * Registers a listener to the exit event that will only be performed once.
@@ -238,7 +247,11 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    */
   readonly resetUsage: () => void
   /**
-   * Gets task usage statistics.
+   * Close communication channel.
+   */
+  readonly closeChannel: () => void
+  /**
+   * Gets task worker usage statistics.
    */
   readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
 }