fix: fixes to pool initialization
[poolifier.git] / src / pools / worker.ts
index be134f05f539b87bf254bbfb71e68c90e360946c..03cecce849a2cb9d71ad04a566795d3588238509 100644 (file)
@@ -132,7 +132,7 @@ export interface WorkerInfo {
   /**
    * Worker type.
    */
-  type: WorkerType
+  readonly type: WorkerType
   /**
    * Dynamic flag.
    */
@@ -144,7 +144,7 @@ export interface WorkerInfo {
   /**
    * Task function names.
    */
-  taskFunctions?: string[]
+  taskFunctionNames?: string[]
 }
 
 /**
@@ -171,6 +171,15 @@ export interface WorkerUsage {
   readonly elu: EventLoopUtilizationMeasurementStatistics
 }
 
+/**
+ * Worker choice strategy data.
+ *
+ * @internal
+ */
+export interface StrategyData {
+  virtualTaskEndTimestamp?: number
+}
+
 /**
  * Worker interface.
  */
@@ -199,7 +208,14 @@ export interface IWorker {
   readonly once: (event: 'exit', handler: ExitHandler<this>) => void
 }
 
-export type WorkerNodeEventCallback = (workerId: number) => void
+/**
+ * Worker node event detail.
+ *
+ * @internal
+ */
+export interface WorkerNodeEventDetail {
+  workerId: number
+}
 
 /**
  * Worker node interface.
@@ -208,7 +224,8 @@ export type WorkerNodeEventCallback = (workerId: number) => void
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
  * @internal
  */
-export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
+export interface IWorkerNode<Worker extends IWorker, Data = unknown>
+  extends EventTarget {
   /**
    * Worker.
    */
@@ -220,7 +237,12 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
   /**
    * Worker usage statistics.
    */
-  usage: WorkerUsage
+  readonly usage: WorkerUsage
+  /**
+   * Worker choice strategy data.
+   * This is used to store data that are specific to the worker choice strategy.
+   */
+  strategyData?: StrategyData
   /**
    * Message channel (worker_threads only).
    */
@@ -230,18 +252,6 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * This is the number of tasks that can be enqueued before the worker node has back pressure.
    */
   tasksQueueBackPressureSize: number
-  /**
-   * Callback invoked when worker node tasks queue is back pressured.
-   *
-   * @param workerId - The worker id.
-   */
-  onBackPressure?: WorkerNodeEventCallback
-  /**
-   * Callback invoked when worker node tasks queue is empty.
-   *
-   * @param workerId - The worker id.
-   */
-  onEmptyQueue?: WorkerNodeEventCallback
   /**
    * Tasks queue size.
    *
@@ -299,4 +309,11 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
    */
   readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
+  /**
+   * Deletes task function worker usage statistics.
+   *
+   * @param name - The task function name.
+   * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
+   */
+  readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean
 }