1 import EventEmitter from
'events'
2 import type { MessageValue
} from
'../utility-types'
3 import type { IWorker
} from
'./abstract-pool'
4 import type { IPool
} from
'./pool'
7 * Internal poolifier pool emitter.
9 export class PoolEmitter
extends EventEmitter
{}
12 * Internal contract definition for a poolifier pool.
14 * @template Worker Type of worker which manages this pool.
15 * @template Data Type of data sent to the worker.
16 * @template Response Type of response of execution.
18 export interface IPoolInternal
<
19 Worker
extends IWorker
,
22 > extends IPool
<Data
, Response
> {
24 * List of currently available workers.
26 readonly workers
: Worker
[]
31 * - `key`: The `Worker`
32 * - `value`: Number of tasks currently in progress on the worker.
34 readonly tasks
: Map
<Worker
, number>
37 * Emitter on which events can be listened to.
39 * Events that can currently be listened to:
43 readonly emitter
: PoolEmitter
46 * Whether the pool is dynamic or not.
48 * If it is dynamic, it provides the `max` property.
50 readonly dynamic
: boolean
53 * Maximum number of workers that can be created by this pool.
58 * Creates a new worker for this pool and sets it up completely.
60 * @returns New, completely set up worker.
62 createAndSetupWorker(): Worker
65 * Shut down given worker.
67 * @param worker A worker within `workers`.
69 destroyWorker(worker
: Worker
): void | Promise
<void>
72 * Register a listener callback on a given worker.
74 * @param worker A worker.
75 * @param listener A message listener callback.
77 registerWorkerMessageListener
<Message
extends Data
| Response
>(
79 listener
: (message
: MessageValue
<Message
>) => void