Commit | Line | Data |
---|---|---|
a35560ba S |
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' | |
5 | ||
6 | /** | |
7 | * Internal poolifier pool emitter. | |
8 | */ | |
9 | export class PoolEmitter extends EventEmitter {} | |
10 | ||
11 | /** | |
12 | * Internal contract definition for a poolifier pool. | |
13 | * | |
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. | |
17 | */ | |
18 | export interface IPoolInternal< | |
19 | Worker extends IWorker, | |
20 | Data = unknown, | |
21 | Response = unknown | |
22 | > extends IPool<Data, Response> { | |
23 | /** | |
24 | * List of currently available workers. | |
25 | */ | |
26 | readonly workers: Worker[] | |
27 | ||
28 | /** | |
29 | * The tasks map. | |
30 | * | |
31 | * - `key`: The `Worker` | |
32 | * - `value`: Number of tasks currently in progress on the worker. | |
33 | */ | |
34 | readonly tasks: Map<Worker, number> | |
35 | ||
36 | /** | |
37 | * Emitter on which events can be listened to. | |
38 | * | |
39 | * Events that can currently be listened to: | |
40 | * | |
41 | * - `'FullPool'` | |
42 | */ | |
43 | readonly emitter: PoolEmitter | |
44 | ||
45 | /** | |
46 | * Maximum number of workers that can be created by this pool. | |
47 | */ | |
48 | readonly max?: number | |
49 | ||
50 | /** | |
51 | * Whether the pool is dynamic or not. | |
52 | * | |
53 | * If it is dynamic, it provides the `max` property. | |
54 | */ | |
55 | isDynamic(): boolean | |
56 | ||
57 | /** | |
58 | * Creates a new worker for this pool and sets it up completely. | |
59 | * | |
60 | * @returns New, completely set up worker. | |
61 | */ | |
62 | createAndSetupWorker(): Worker | |
63 | ||
64 | /** | |
65 | * Shut down given worker. | |
66 | * | |
67 | * @param worker A worker within `workers`. | |
68 | */ | |
69 | destroyWorker(worker: Worker): void | Promise<void> | |
70 | ||
71 | /** | |
72 | * Register a listener callback on a given worker. | |
73 | * | |
74 | * @param worker A worker. | |
75 | * @param listener A message listener callback. | |
76 | */ | |
77 | registerWorkerMessageListener<Message extends Data | Response>( | |
78 | worker: Worker, | |
79 | listener: (message: MessageValue<Message>) => void | |
80 | ): void | |
81 | } |