import EventEmitter from 'events'
-import type { IWorker } from './abstract-pool'
import type { IPool } from './pool'
+import type { IPoolWorker } from './pool-worker'
/**
* Pool types.
DYNAMIC = 'dynamic'
}
+/**
+ * Tasks usage statistics.
+ */
+export interface TasksUsage {
+ run: number
+ running: number
+ runTime: number
+ avgRunTime: number
+}
+
/**
* Internal poolifier pool emitter.
*/
* @template Response Type of response of execution.
*/
export interface IPoolInternal<
- Worker extends IWorker,
+ Worker extends IPoolWorker,
Data = unknown,
Response = unknown
> extends IPool<Data, Response> {
readonly workers: Worker[]
/**
- * The tasks map.
+ * The workers tasks usage map.
*
- * - `key`: The `Worker`
- * - `value`: Number of tasks currently in progress on the worker.
+ * `key`: The `Worker`
+ * `value`: Worker tasks usage statistics.
*/
- readonly tasks: Map<Worker, number>
+ readonly workersTasksUsage: Map<Worker, TasksUsage>
/**
* Emitter on which events can be listened to.
readonly numberOfRunningTasks: number
/**
- * Find a tasks map entry with a free worker based on the number of tasks the worker has applied.
+ * Finds 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
+
+ /**
+ * Gets worker index.
*
- * If an entry is found with a worker that has `0` tasks, it is detected as free.
+ * @param worker The worker.
+ * @returns The worker index.
+ */
+ getWorkerIndex(worker: Worker): number
+
+ /**
+ * Gets worker running tasks.
*
- * If no tasks map entry with a free worker was found, `false` will be returned.
+ * @param worker The worker.
+ * @returns The number of tasks currently running on the worker.
+ */
+ getWorkerRunningTasks(worker: Worker): number | undefined
+
+ /**
+ * Gets worker average tasks runtime.
*
- * @returns A tasks map entry with a free worker if there was one, otherwise `false`.
+ * @param worker The worker.
+ * @returns The average tasks runtime on the worker.
*/
- findFreeTasksMapEntry(): [Worker, number] | false
+ getWorkerAverageTasksRunTime(worker: Worker): number | undefined
}