Comment spell fixlet
[poolifier.git] / src / pools / pool-internal.ts
index 911e761df157a07f54ac1300328c82548cc6d9d6..2cf84adbc6bfd28e9d552795cbb3b49bbe8ffee4 100644 (file)
@@ -1,7 +1,25 @@
 import EventEmitter from 'events'
-import type { IWorker } from './abstract-pool'
+import type { AbstractPoolWorker } from './abstract-pool-worker'
 import type { IPool } from './pool'
 
+/**
+ * Pool types.
+ */
+export enum PoolType {
+  FIXED = 'fixed',
+  DYNAMIC = 'dynamic'
+}
+
+/**
+ * Tasks usage statistics.
+ */
+export interface TasksUsage {
+  run: number
+  running: number
+  runTime: number
+  avgRunTime: number
+}
+
 /**
  * Internal poolifier pool emitter.
  */
@@ -15,7 +33,7 @@ export class PoolEmitter extends EventEmitter {}
  * @template Response Type of response of execution.
  */
 export interface IPoolInternal<
-  Worker extends IWorker,
+  Worker extends AbstractPoolWorker,
   Data = unknown,
   Response = unknown
 > extends IPool<Data, Response> {
@@ -24,14 +42,6 @@ export interface IPoolInternal<
    */
   readonly workers: Worker[]
 
-  /**
-   * The tasks map.
-   *
-   * - `key`: The `Worker`
-   * - `value`: Number of tasks currently in progress on the worker.
-   */
-  readonly tasks: Map<Worker, number>
-
   /**
    * Emitter on which events can be listened to.
    *
@@ -39,17 +49,64 @@ export interface IPoolInternal<
    *
    * - `'busy'`
    */
-  readonly emitter: PoolEmitter
+  readonly emitter?: PoolEmitter
 
   /**
-   * Whether the pool is dynamic or not.
+   * Pool type.
    *
-   * If it is dynamic, it provides the `max` property.
+   * If it is `'dynamic'`, it provides the `max` property.
    */
-  readonly dynamic: boolean
+  readonly type: PoolType
 
   /**
    * Maximum number of workers that can be created by this pool.
    */
   readonly max?: number
+
+  /**
+   * Whether the pool is busy or not.
+   *
+   * The pool busyness boolean status.
+   */
+  readonly busy: boolean
+
+  /**
+   * Number of tasks currently concurrently running.
+   */
+  readonly numberOfRunningTasks: number
+
+  /**
+   * Find a free worker based on the number of tasks the worker has applied.
+   *
+   * If a worker is found with `0` running tasks, it is detected as free and returned.
+   *
+   * If no free worker is found, `false` is returned.
+   *
+   * @returns A free worker if there is one, otherwise `false`.
+   */
+  findFreeWorker(): Worker | false
+
+  /**
+   * Get worker index.
+   *
+   * @param worker The worker.
+   * @returns The worker index.
+   */
+  getWorkerIndex(worker: Worker): number
+
+  /**
+   * Get worker running tasks.
+   *
+   * @param worker The worker.
+   * @returns The number of tasks currently running on the worker.
+   */
+  getWorkerRunningTasks(worker: Worker): number | undefined
+
+  /**
+   * Get worker average tasks runtime.
+   *
+   * @param worker The worker.
+   * @returns The average tasks runtime on the worker.
+   */
+  getWorkerAverageTasksRunTime(worker: Worker): number | undefined
 }