poolifier 4.2.3 → 4.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { EventEmitter, EventEmitterAsyncResource } from 'node:events';
2
1
  import { TransferListItem, MessagePort, WorkerOptions as WorkerOptions$1, MessageChannel, Worker as Worker$1 } from 'node:worker_threads';
2
+ import { EventEmitter, EventEmitterAsyncResource } from 'node:events';
3
3
  import { AsyncResource } from 'node:async_hooks';
4
4
  import { EventLoopUtilization } from 'node:perf_hooks';
5
5
  import { ClusterSettings, Worker } from 'node:cluster';
@@ -9,16 +9,21 @@ import { ClusterSettings, Worker } from 'node:cluster';
9
9
  * @internal
10
10
  */
11
11
  declare class CircularBuffer {
12
- private readIdx;
13
- private writeIdx;
14
12
  private readonly items;
15
13
  private readonly maxArrayIdx;
14
+ private readIdx;
15
+ private writeIdx;
16
16
  size: number;
17
17
  /**
18
18
  * @param size - Buffer size. @defaultValue defaultBufferSize
19
19
  * @returns CircularBuffer.
20
20
  */
21
21
  constructor(size?: number);
22
+ /**
23
+ * Checks the buffer size.
24
+ * @param size - Buffer size.
25
+ */
26
+ private checkSize;
22
27
  /**
23
28
  * Checks whether the buffer is empty.
24
29
  * @returns Whether the buffer is empty.
@@ -29,39 +34,34 @@ declare class CircularBuffer {
29
34
  * @returns Whether the buffer is full.
30
35
  */
31
36
  full(): boolean;
32
- /**
33
- * Puts number into buffer.
34
- * @param number - Number to put into buffer.
35
- */
36
- put(number: number): void;
37
37
  /**
38
38
  * Gets number from buffer.
39
39
  * @returns Number from buffer.
40
40
  */
41
41
  get(): number | undefined;
42
+ /**
43
+ * Puts number into buffer.
44
+ * @param number - Number to put into buffer.
45
+ */
46
+ put(number: number): void;
42
47
  /**
43
48
  * Returns buffer as numbers' array.
44
49
  * @returns Numbers' array.
45
50
  */
46
51
  toArray(): number[];
47
- /**
48
- * Checks the buffer size.
49
- * @param size - Buffer size.
50
- */
51
- private checkSize;
52
52
  }
53
53
 
54
54
  /**
55
55
  * Enumeration of worker choice strategies.
56
56
  */
57
57
  declare const WorkerChoiceStrategies: Readonly<{
58
- ROUND_ROBIN: 'ROUND_ROBIN';
59
- LEAST_USED: 'LEAST_USED';
58
+ FAIR_SHARE: 'FAIR_SHARE';
59
+ INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN';
60
60
  LEAST_BUSY: 'LEAST_BUSY';
61
61
  LEAST_ELU: 'LEAST_ELU';
62
- FAIR_SHARE: 'FAIR_SHARE';
62
+ LEAST_USED: 'LEAST_USED';
63
+ ROUND_ROBIN: 'ROUND_ROBIN';
63
64
  WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN';
64
- INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN';
65
65
  }>;
66
66
  /**
67
67
  * Worker choice strategy.
@@ -71,9 +71,9 @@ type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies;
71
71
  * Enumeration of measurements.
72
72
  */
73
73
  declare const Measurements: Readonly<{
74
+ elu: 'elu';
74
75
  runTime: 'runTime';
75
76
  waitTime: 'waitTime';
76
- elu: 'elu';
77
77
  }>;
78
78
  /**
79
79
  * Measurement.
@@ -92,6 +92,11 @@ interface MeasurementOptions {
92
92
  * Worker choice strategy options.
93
93
  */
94
94
  interface WorkerChoiceStrategyOptions {
95
+ /**
96
+ * Event loop utilization options.
97
+ * @defaultValue \{ median: false \}
98
+ */
99
+ readonly elu?: MeasurementOptions;
95
100
  /**
96
101
  * Measurement to use in worker choice strategy supporting it.
97
102
  */
@@ -106,11 +111,6 @@ interface WorkerChoiceStrategyOptions {
106
111
  * @defaultValue \{ median: false \}
107
112
  */
108
113
  readonly waitTime?: MeasurementOptions;
109
- /**
110
- * Event loop utilization options.
111
- * @defaultValue \{ median: false \}
112
- */
113
- readonly elu?: MeasurementOptions;
114
114
  /**
115
115
  * Worker weights to use for weighted round robin worker selection strategies.
116
116
  * A weight is tasks maximum execution time in milliseconds for a worker node.
@@ -141,6 +141,10 @@ interface MeasurementStatisticsRequirements {
141
141
  * @internal
142
142
  */
143
143
  interface TaskStatisticsRequirements {
144
+ /**
145
+ * Tasks event loop utilization requirements.
146
+ */
147
+ readonly elu: MeasurementStatisticsRequirements;
144
148
  /**
145
149
  * Tasks runtime requirements.
146
150
  */
@@ -149,49 +153,26 @@ interface TaskStatisticsRequirements {
149
153
  * Tasks wait time requirements.
150
154
  */
151
155
  readonly waitTime: MeasurementStatisticsRequirements;
152
- /**
153
- * Tasks event loop utilization requirements.
154
- */
155
- readonly elu: MeasurementStatisticsRequirements;
156
156
  }
157
157
  /**
158
158
  * Strategy policy.
159
159
  * @internal
160
160
  */
161
161
  interface StrategyPolicy {
162
- /**
163
- * Expects tasks execution on the newly created dynamic worker.
164
- */
165
- readonly dynamicWorkerUsage: boolean;
166
162
  /**
167
163
  * Expects the newly created dynamic worker to be flagged as ready.
168
164
  */
169
165
  readonly dynamicWorkerReady: boolean;
166
+ /**
167
+ * Expects tasks execution on the newly created dynamic worker.
168
+ */
169
+ readonly dynamicWorkerUsage: boolean;
170
170
  }
171
171
  /**
172
172
  * Worker choice strategy interface.
173
173
  * @internal
174
174
  */
175
175
  interface IWorkerChoiceStrategy {
176
- /**
177
- * Strategy policy.
178
- */
179
- readonly strategyPolicy: StrategyPolicy;
180
- /**
181
- * Tasks statistics requirements.
182
- */
183
- readonly taskStatisticsRequirements: TaskStatisticsRequirements;
184
- /**
185
- * Resets strategy internals.
186
- * @returns `true` if the reset is successful, `false` otherwise.
187
- */
188
- readonly reset: () => boolean;
189
- /**
190
- * Updates the worker node key strategy internals.
191
- * This is called after a task has been executed on a worker node.
192
- * @returns `true` if the update is successful, `false` otherwise.
193
- */
194
- readonly update: (workerNodeKey: number) => boolean;
195
176
  /**
196
177
  * Chooses a worker node in the pool and returns its key.
197
178
  * If no worker nodes are not eligible, `undefined` is returned.
@@ -205,19 +186,38 @@ interface IWorkerChoiceStrategy {
205
186
  * @returns `true` if the worker node key is removed, `false` otherwise.
206
187
  */
207
188
  readonly remove: (workerNodeKey: number) => boolean;
189
+ /**
190
+ * Resets strategy internals.
191
+ * @returns `true` if the reset is successful, `false` otherwise.
192
+ */
193
+ readonly reset: () => boolean;
208
194
  /**
209
195
  * Sets the worker choice strategy options.
210
196
  * @param opts - The worker choice strategy options.
211
197
  */
212
- readonly setOptions: (opts: WorkerChoiceStrategyOptions | undefined) => void;
198
+ readonly setOptions: (opts: undefined | WorkerChoiceStrategyOptions) => void;
199
+ /**
200
+ * Strategy policy.
201
+ */
202
+ readonly strategyPolicy: StrategyPolicy;
203
+ /**
204
+ * Tasks statistics requirements.
205
+ */
206
+ readonly taskStatisticsRequirements: TaskStatisticsRequirements;
207
+ /**
208
+ * Updates the worker node key strategy internals.
209
+ * This is called after a task has been executed on a worker node.
210
+ * @returns `true` if the update is successful, `false` otherwise.
211
+ */
212
+ readonly update: (workerNodeKey: number) => boolean;
213
213
  }
214
214
 
215
215
  /**
216
216
  * Enumeration of kill behaviors.
217
217
  */
218
218
  declare const KillBehaviors: Readonly<{
219
- SOFT: 'SOFT';
220
219
  HARD: 'HARD';
220
+ SOFT: 'SOFT';
221
221
  }>;
222
222
  /**
223
223
  * Kill behavior.
@@ -226,7 +226,7 @@ type KillBehavior = keyof typeof KillBehaviors;
226
226
  /**
227
227
  * Handler called when a worker is killed.
228
228
  */
229
- type KillHandler = () => void | Promise<void>;
229
+ type KillHandler = () => Promise<void> | void;
230
230
  /**
231
231
  * Options for workers.
232
232
  */
@@ -241,6 +241,11 @@ interface WorkerOptions {
241
241
  * @defaultValue KillBehaviors.SOFT
242
242
  */
243
243
  killBehavior?: KillBehavior;
244
+ /**
245
+ * The function to call when a worker is killed.
246
+ * @defaultValue `() => {}`
247
+ */
248
+ killHandler?: KillHandler;
244
249
  /**
245
250
  * Maximum waiting time in milliseconds for tasks on newly created workers. It must be greater or equal than 5.
246
251
  *
@@ -253,11 +258,6 @@ interface WorkerOptions {
253
258
  * @defaultValue 60000
254
259
  */
255
260
  maxInactiveTime?: number;
256
- /**
257
- * The function to call when a worker is killed.
258
- * @defaultValue `() => {}`
259
- */
260
- killHandler?: KillHandler;
261
261
  }
262
262
 
263
263
  /**
@@ -266,17 +266,17 @@ interface WorkerOptions {
266
266
  */
267
267
  interface WorkerError<Data = unknown> {
268
268
  /**
269
- * Task function name triggering the error.
269
+ * Data triggering the error.
270
270
  */
271
- readonly name: string;
271
+ readonly data?: Data;
272
272
  /**
273
273
  * Error message.
274
274
  */
275
275
  readonly message: string;
276
276
  /**
277
- * Data triggering the error.
277
+ * Task function name triggering the error.
278
278
  */
279
- readonly data?: Data;
279
+ readonly name: string;
280
280
  }
281
281
  /**
282
282
  * Task performance.
@@ -284,35 +284,35 @@ interface WorkerError<Data = unknown> {
284
284
  */
285
285
  interface TaskPerformance {
286
286
  /**
287
- * Task name.
287
+ * Task event loop utilization.
288
288
  */
289
- readonly name: string;
289
+ readonly elu?: EventLoopUtilization;
290
290
  /**
291
- * Task performance timestamp.
291
+ * Task name.
292
292
  */
293
- readonly timestamp: number;
293
+ readonly name: string;
294
294
  /**
295
295
  * Task runtime.
296
296
  */
297
297
  readonly runTime?: number;
298
298
  /**
299
- * Task event loop utilization.
299
+ * Task performance timestamp.
300
300
  */
301
- readonly elu?: EventLoopUtilization;
301
+ readonly timestamp: number;
302
302
  }
303
303
  /**
304
304
  * Worker task performance statistics computation settings.
305
305
  * @internal
306
306
  */
307
307
  interface WorkerStatistics {
308
- /**
309
- * Whether the worker computes the task runtime or not.
310
- */
311
- readonly runTime: boolean;
312
308
  /**
313
309
  * Whether the worker computes the task event loop utilization (ELU) or not.
314
310
  */
315
311
  readonly elu: boolean;
312
+ /**
313
+ * Whether the worker computes the task runtime or not.
314
+ */
315
+ readonly runTime: boolean;
316
316
  }
317
317
  /**
318
318
  * Task function properties.
@@ -337,14 +337,14 @@ interface TaskFunctionProperties {
337
337
  * @internal
338
338
  */
339
339
  interface Task<Data = unknown> {
340
- /**
341
- * Task name.
342
- */
343
- readonly name?: string;
344
340
  /**
345
341
  * Task input data that will be passed to the worker.
346
342
  */
347
343
  readonly data?: Data;
344
+ /**
345
+ * Task name.
346
+ */
347
+ readonly name?: string;
348
348
  /**
349
349
  * Task priority. Lower values have higher priority.
350
350
  * @defaultValue 0
@@ -355,17 +355,17 @@ interface Task<Data = unknown> {
355
355
  */
356
356
  readonly strategy?: WorkerChoiceStrategy;
357
357
  /**
358
- * Array of transferable objects.
358
+ * Task UUID.
359
359
  */
360
- readonly transferList?: readonly TransferListItem[];
360
+ readonly taskId?: `${string}-${string}-${string}-${string}-${string}`;
361
361
  /**
362
362
  * Timestamp.
363
363
  */
364
364
  readonly timestamp?: number;
365
365
  /**
366
- * Task UUID.
366
+ * Array of transferable objects.
367
367
  */
368
- readonly taskId?: `${string}-${string}-${string}-${string}-${string}`;
368
+ readonly transferList?: readonly TransferListItem[];
369
369
  }
370
370
  /**
371
371
  * Message object that is passed between main worker and worker.
@@ -375,28 +375,36 @@ interface Task<Data = unknown> {
375
375
  */
376
376
  interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
377
377
  /**
378
- * Worker id.
378
+ * Whether the worker starts or stops its activity check.
379
379
  */
380
- readonly workerId?: number;
380
+ readonly checkActive?: boolean;
381
381
  /**
382
382
  * Kill code.
383
383
  */
384
- readonly kill?: KillBehavior | true | 'success' | 'failure';
384
+ readonly kill?: 'failure' | 'success' | KillBehavior | true;
385
385
  /**
386
- * Worker error.
386
+ * Message port.
387
387
  */
388
- readonly workerError?: WorkerError<ErrorData>;
388
+ readonly port?: MessagePort;
389
389
  /**
390
- * Task performance.
390
+ * Whether the worker is ready or not.
391
391
  */
392
- readonly taskPerformance?: TaskPerformance;
392
+ readonly ready?: boolean;
393
+ /**
394
+ * Whether the worker computes the given statistics or not.
395
+ */
396
+ readonly statistics?: WorkerStatistics;
397
+ /**
398
+ * Task function serialized to string.
399
+ */
400
+ readonly taskFunction?: string;
393
401
  /**
394
402
  * Task function operation:
395
403
  * - `'add'` - Add a task function.
396
404
  * - `'remove'` - Remove a task function.
397
405
  * - `'default'` - Set a task function as default.
398
406
  */
399
- readonly taskFunctionOperation?: 'add' | 'remove' | 'default';
407
+ readonly taskFunctionOperation?: 'add' | 'default' | 'remove';
400
408
  /**
401
409
  * Whether the task function operation is successful or not.
402
410
  */
@@ -405,30 +413,22 @@ interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
405
413
  * Task function properties.
406
414
  */
407
415
  readonly taskFunctionProperties?: TaskFunctionProperties;
408
- /**
409
- * Task function serialized to string.
410
- */
411
- readonly taskFunction?: string;
412
416
  /**
413
417
  * Task functions properties.
414
418
  */
415
419
  readonly taskFunctionsProperties?: TaskFunctionProperties[];
416
420
  /**
417
- * Whether the worker computes the given statistics or not.
418
- */
419
- readonly statistics?: WorkerStatistics;
420
- /**
421
- * Whether the worker is ready or not.
421
+ * Task performance.
422
422
  */
423
- readonly ready?: boolean;
423
+ readonly taskPerformance?: TaskPerformance;
424
424
  /**
425
- * Whether the worker starts or stops its activity check.
425
+ * Worker error.
426
426
  */
427
- readonly checkActive?: boolean;
427
+ readonly workerError?: WorkerError<ErrorData>;
428
428
  /**
429
- * Message port.
429
+ * Worker id.
430
430
  */
431
- readonly port?: MessagePort;
431
+ readonly workerId?: number;
432
432
  }
433
433
  /**
434
434
  * An object holding the task execution response promise resolve/reject callbacks.
@@ -437,21 +437,21 @@ interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
437
437
  */
438
438
  interface PromiseResponseWrapper<Response = unknown> {
439
439
  /**
440
- * Resolve callback to fulfill the promise.
440
+ * The asynchronous resource used to track the task execution.
441
441
  */
442
- readonly resolve: (value: Response | PromiseLike<Response>) => void;
442
+ readonly asyncResource?: AsyncResource;
443
443
  /**
444
444
  * Reject callback to reject the promise.
445
445
  */
446
446
  readonly reject: (reason?: unknown) => void;
447
447
  /**
448
- * The worker node key executing the task.
448
+ * Resolve callback to fulfill the promise.
449
449
  */
450
- readonly workerNodeKey: number;
450
+ readonly resolve: (value: PromiseLike<Response> | Response) => void;
451
451
  /**
452
- * The asynchronous resource used to track the task execution.
452
+ * The worker node key executing the task.
453
453
  */
454
- readonly asyncResource?: AsyncResource;
454
+ readonly workerNodeKey: number;
455
455
  }
456
456
  /**
457
457
  * Remove readonly modifier from all properties of T.
@@ -485,17 +485,13 @@ type TaskAsyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Pr
485
485
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
486
486
  * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
487
487
  */
488
- type TaskFunction<Data = unknown, Response = unknown> = TaskSyncFunction<Data, Response> | TaskAsyncFunction<Data, Response>;
488
+ type TaskFunction<Data = unknown, Response = unknown> = TaskAsyncFunction<Data, Response> | TaskSyncFunction<Data, Response>;
489
489
  /**
490
490
  * Task function object.
491
491
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
492
492
  * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
493
493
  */
494
494
  interface TaskFunctionObject<Data = unknown, Response = unknown> {
495
- /**
496
- * Task function.
497
- */
498
- taskFunction: TaskFunction<Data, Response>;
499
495
  /**
500
496
  * Task function priority. Lower values have higher priority.
501
497
  */
@@ -504,6 +500,10 @@ interface TaskFunctionObject<Data = unknown, Response = unknown> {
504
500
  * Task function worker choice strategy.
505
501
  */
506
502
  strategy?: WorkerChoiceStrategy;
503
+ /**
504
+ * Task function.
505
+ */
506
+ taskFunction: TaskFunction<Data, Response>;
507
507
  }
508
508
  /**
509
509
  * Tasks functions that can be executed.
@@ -517,8 +517,8 @@ type TaskFunctions<Data = unknown, Response = unknown> = Record<string, TaskFunc
517
517
  * Task function operation result.
518
518
  */
519
519
  interface TaskFunctionOperationResult {
520
- status: boolean;
521
520
  error?: Error;
521
+ status: boolean;
522
522
  }
523
523
 
524
524
  /**
@@ -545,7 +545,7 @@ type ExitHandler<Worker extends IWorker> = (this: Worker, exitCode: number) => v
545
545
  * Worker event handler.
546
546
  * @typeParam Worker - Type of worker.
547
547
  */
548
- type EventHandler<Worker extends IWorker> = OnlineHandler<Worker> | MessageHandler<Worker> | ErrorHandler<Worker> | ExitHandler<Worker>;
548
+ type EventHandler<Worker extends IWorker> = ErrorHandler<Worker> | ExitHandler<Worker> | MessageHandler<Worker> | OnlineHandler<Worker>;
549
549
  /**
550
550
  * Measurement statistics.
551
551
  * @internal
@@ -556,33 +556,33 @@ interface MeasurementStatistics {
556
556
  */
557
557
  aggregate?: number;
558
558
  /**
559
- * Measurement minimum.
559
+ * Measurement average.
560
560
  */
561
- minimum?: number;
561
+ average?: number;
562
562
  /**
563
- * Measurement maximum.
563
+ * Measurement history.
564
564
  */
565
- maximum?: number;
565
+ readonly history: CircularBuffer;
566
566
  /**
567
- * Measurement average.
567
+ * Measurement maximum.
568
568
  */
569
- average?: number;
569
+ maximum?: number;
570
570
  /**
571
571
  * Measurement median.
572
572
  */
573
573
  median?: number;
574
574
  /**
575
- * Measurement history.
575
+ * Measurement minimum.
576
576
  */
577
- readonly history: CircularBuffer;
577
+ minimum?: number;
578
578
  }
579
579
  /**
580
580
  * Event loop utilization measurement statistics.
581
581
  * @internal
582
582
  */
583
583
  interface EventLoopUtilizationMeasurementStatistics {
584
- readonly idle: MeasurementStatistics;
585
584
  readonly active: MeasurementStatistics;
585
+ readonly idle: MeasurementStatistics;
586
586
  utilization?: number;
587
587
  }
588
588
  /**
@@ -599,13 +599,17 @@ interface TaskStatistics {
599
599
  */
600
600
  executing: number;
601
601
  /**
602
- * Number of queued tasks.
602
+ * Number of failed tasks.
603
603
  */
604
- readonly queued: number;
604
+ failed: number;
605
605
  /**
606
606
  * Maximum number of queued tasks.
607
607
  */
608
608
  readonly maxQueued?: number;
609
+ /**
610
+ * Number of queued tasks.
611
+ */
612
+ readonly queued: number;
609
613
  /**
610
614
  * Number of sequentially stolen tasks.
611
615
  */
@@ -614,17 +618,13 @@ interface TaskStatistics {
614
618
  * Number of stolen tasks.
615
619
  */
616
620
  stolen: number;
617
- /**
618
- * Number of failed tasks.
619
- */
620
- failed: number;
621
621
  }
622
622
  /**
623
623
  * Enumeration of worker types.
624
624
  */
625
625
  declare const WorkerTypes: Readonly<{
626
- thread: 'thread';
627
626
  cluster: 'cluster';
627
+ thread: 'thread';
628
628
  }>;
629
629
  /**
630
630
  * Worker type.
@@ -636,17 +636,28 @@ type WorkerType = keyof typeof WorkerTypes;
636
636
  */
637
637
  interface WorkerInfo {
638
638
  /**
639
- * Worker id.
639
+ * Back pressure flag.
640
+ * This flag is set to `true` when worker node tasks queue has back pressure.
640
641
  */
641
- readonly id: number | undefined;
642
+ backPressure: boolean;
642
643
  /**
643
- * Worker type.
644
+ * Back pressure stealing flag.
645
+ * This flag is set to `true` when worker node is stealing one task from another back pressured worker node.
644
646
  */
645
- readonly type: WorkerType;
647
+ backPressureStealing: boolean;
648
+ /**
649
+ * Continuous stealing flag.
650
+ * This flag is set to `true` when worker node is continuously stealing tasks from other worker nodes.
651
+ */
652
+ continuousStealing: boolean;
646
653
  /**
647
654
  * Dynamic flag.
648
655
  */
649
656
  dynamic: boolean;
657
+ /**
658
+ * Worker id.
659
+ */
660
+ readonly id: number | undefined;
650
661
  /**
651
662
  * Ready flag.
652
663
  */
@@ -661,25 +672,14 @@ interface WorkerInfo {
661
672
  * This flag is set to `true` when worker node has one task stolen from another worker node.
662
673
  */
663
674
  stolen: boolean;
664
- /**
665
- * Continuous stealing flag.
666
- * This flag is set to `true` when worker node is continuously stealing tasks from other worker nodes.
667
- */
668
- continuousStealing: boolean;
669
- /**
670
- * Back pressure stealing flag.
671
- * This flag is set to `true` when worker node is stealing one task from another back pressured worker node.
672
- */
673
- backPressureStealing: boolean;
674
- /**
675
- * Back pressure flag.
676
- * This flag is set to `true` when worker node tasks queue has back pressure.
677
- */
678
- backPressure: boolean;
679
675
  /**
680
676
  * Task functions properties.
681
677
  */
682
678
  taskFunctionsProperties?: TaskFunctionProperties[];
679
+ /**
680
+ * Worker type.
681
+ */
682
+ readonly type: WorkerType;
683
683
  }
684
684
  /**
685
685
  * Worker usage statistics.
@@ -687,21 +687,21 @@ interface WorkerInfo {
687
687
  */
688
688
  interface WorkerUsage {
689
689
  /**
690
- * Tasks statistics.
690
+ * Tasks event loop utilization statistics.
691
691
  */
692
- readonly tasks: TaskStatistics;
692
+ readonly elu: EventLoopUtilizationMeasurementStatistics;
693
693
  /**
694
694
  * Tasks runtime statistics.
695
695
  */
696
696
  readonly runTime: MeasurementStatistics;
697
697
  /**
698
- * Tasks wait time statistics.
698
+ * Tasks statistics.
699
699
  */
700
- readonly waitTime: MeasurementStatistics;
700
+ readonly tasks: TaskStatistics;
701
701
  /**
702
- * Tasks event loop utilization statistics.
702
+ * Tasks wait time statistics.
703
703
  */
704
- readonly elu: EventLoopUtilizationMeasurementStatistics;
704
+ readonly waitTime: MeasurementStatistics;
705
705
  }
706
706
  /**
707
707
  * Worker choice strategy data.
@@ -714,14 +714,18 @@ interface StrategyData {
714
714
  * Worker interface.
715
715
  */
716
716
  interface IWorker extends EventEmitter {
717
+ /**
718
+ * Cluster worker disconnect.
719
+ */
720
+ readonly disconnect?: () => void;
717
721
  /**
718
722
  * Cluster worker id.
719
723
  */
720
724
  readonly id?: number;
721
725
  /**
722
- * Worker thread worker id.
726
+ * Cluster worker kill.
723
727
  */
724
- readonly threadId?: number;
728
+ readonly kill?: (signal?: string) => void;
725
729
  /**
726
730
  * Registers an event handler.
727
731
  * @param event - The event.
@@ -734,36 +738,32 @@ interface IWorker extends EventEmitter {
734
738
  * @param handler - The event handler.
735
739
  */
736
740
  readonly once: (event: string, handler: EventHandler<this>) => this;
737
- /**
738
- * Calling `unref()` on a worker allows the thread to exit if this is the only
739
- * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
740
- * @since v10.5.0
741
- */
742
- readonly unref?: () => void;
743
741
  /**
744
742
  * Stop all JavaScript execution in the worker thread as soon as possible.
745
743
  * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
746
744
  */
747
745
  readonly terminate?: () => Promise<number>;
748
746
  /**
749
- * Cluster worker disconnect.
747
+ * Worker thread worker id.
750
748
  */
751
- readonly disconnect?: () => void;
749
+ readonly threadId?: number;
752
750
  /**
753
- * Cluster worker kill.
751
+ * Calling `unref()` on a worker allows the thread to exit if this is the only
752
+ * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
753
+ * @since v10.5.0
754
754
  */
755
- readonly kill?: (signal?: string) => void;
755
+ readonly unref?: () => void;
756
756
  }
757
757
  /**
758
758
  * Worker node options.
759
759
  * @internal
760
760
  */
761
761
  interface WorkerNodeOptions {
762
- workerOptions?: WorkerOptions$1;
763
762
  env?: Record<string, unknown>;
764
763
  tasksQueueBackPressureSize: number | undefined;
765
764
  tasksQueueBucketSize: number | undefined;
766
765
  tasksQueuePriority: boolean | undefined;
766
+ workerOptions?: WorkerOptions$1;
767
767
  }
768
768
  /**
769
769
  * Worker node interface.
@@ -773,41 +773,26 @@ interface WorkerNodeOptions {
773
773
  */
774
774
  interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitter {
775
775
  /**
776
- * Worker.
777
- */
778
- readonly worker: Worker;
779
- /**
780
- * Worker info.
781
- */
782
- readonly info: WorkerInfo;
783
- /**
784
- * Worker usage statistics.
785
- */
786
- readonly usage: WorkerUsage;
787
- /**
788
- * Worker choice strategy data.
789
- * This is used to store data that are specific to the worker choice strategy.
790
- */
791
- strategyData?: StrategyData;
792
- /**
793
- * Message channel (worker thread only).
776
+ * Clears tasks queue.
794
777
  */
795
- readonly messageChannel?: MessageChannel;
778
+ readonly clearTasksQueue: () => void;
796
779
  /**
797
- * Tasks queue back pressure size.
798
- * This is the number of tasks that can be enqueued before the worker node has back pressure.
780
+ * Deletes task function worker usage statistics.
781
+ * @param name - The task function name.
782
+ * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
799
783
  */
800
- tasksQueueBackPressureSize: number;
784
+ readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean;
801
785
  /**
802
- * Sets tasks queue priority.
803
- * @param enablePriority - Whether to enable tasks queue priority.
786
+ * Dequeue last prioritized task.
787
+ * @returns The dequeued task.
804
788
  */
805
- readonly setTasksQueuePriority: (enablePriority: boolean) => void;
789
+ readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined;
806
790
  /**
807
- * Tasks queue size.
808
- * @returns The tasks queue size.
791
+ * Dequeue task.
792
+ * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
793
+ * @returns The dequeued task.
809
794
  */
810
- readonly tasksQueueSize: () => number;
795
+ readonly dequeueTask: (bucket?: number) => Task<Data> | undefined;
811
796
  /**
812
797
  * Enqueue task.
813
798
  * @param task - The task to queue.
@@ -815,29 +800,30 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
815
800
  */
816
801
  readonly enqueueTask: (task: Task<Data>) => number;
817
802
  /**
818
- * Dequeue task.
819
- * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
820
- * @returns The dequeued task.
803
+ * Gets task function worker usage statistics.
804
+ * @param name - The task function name.
805
+ * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
821
806
  */
822
- readonly dequeueTask: (bucket?: number) => Task<Data> | undefined;
807
+ readonly getTaskFunctionWorkerUsage: (name: string) => undefined | WorkerUsage;
823
808
  /**
824
- * Dequeue last prioritized task.
825
- * @returns The dequeued task.
809
+ * Whether the worker node has back pressure (i.e. its tasks queue is full).
810
+ * @returns `true` if the worker node has back pressure, `false` otherwise.
826
811
  */
827
- readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined;
812
+ readonly hasBackPressure: () => boolean;
828
813
  /**
829
- * Clears tasks queue.
814
+ * Worker info.
830
815
  */
831
- readonly clearTasksQueue: () => void;
816
+ readonly info: WorkerInfo;
832
817
  /**
833
- * Whether the worker node has back pressure (i.e. its tasks queue is full).
834
- * @returns `true` if the worker node has back pressure, `false` otherwise.
818
+ * Message channel (worker thread only).
835
819
  */
836
- readonly hasBackPressure: () => boolean;
820
+ readonly messageChannel?: MessageChannel;
837
821
  /**
838
- * Terminates the worker node.
822
+ * Registers once a worker event handler.
823
+ * @param event - The event.
824
+ * @param handler - The event handler.
839
825
  */
840
- readonly terminate: () => Promise<void>;
826
+ readonly registerOnceWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
841
827
  /**
842
828
  * Registers a worker event handler.
843
829
  * @param event - The event.
@@ -845,23 +831,37 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
845
831
  */
846
832
  readonly registerWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
847
833
  /**
848
- * Registers once a worker event handler.
849
- * @param event - The event.
850
- * @param handler - The event handler.
834
+ * Sets tasks queue priority.
835
+ * @param enablePriority - Whether to enable tasks queue priority.
851
836
  */
852
- readonly registerOnceWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
837
+ readonly setTasksQueuePriority: (enablePriority: boolean) => void;
853
838
  /**
854
- * Gets task function worker usage statistics.
855
- * @param name - The task function name.
856
- * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
839
+ * Worker choice strategy data.
840
+ * This is used to store data that are specific to the worker choice strategy.
857
841
  */
858
- readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined;
842
+ strategyData?: StrategyData;
859
843
  /**
860
- * Deletes task function worker usage statistics.
861
- * @param name - The task function name.
862
- * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
844
+ * Tasks queue back pressure size.
845
+ * This is the number of tasks that can be enqueued before the worker node has back pressure.
863
846
  */
864
- readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean;
847
+ tasksQueueBackPressureSize: number;
848
+ /**
849
+ * Tasks queue size.
850
+ * @returns The tasks queue size.
851
+ */
852
+ readonly tasksQueueSize: () => number;
853
+ /**
854
+ * Terminates the worker node.
855
+ */
856
+ readonly terminate: () => Promise<void>;
857
+ /**
858
+ * Worker usage statistics.
859
+ */
860
+ readonly usage: WorkerUsage;
861
+ /**
862
+ * Worker.
863
+ */
864
+ readonly worker: Worker;
865
865
  }
866
866
  /**
867
867
  * Worker node event detail.
@@ -876,8 +876,8 @@ interface WorkerNodeEventDetail {
876
876
  * Enumeration of pool types.
877
877
  */
878
878
  declare const PoolTypes: Readonly<{
879
- fixed: 'fixed';
880
879
  dynamic: 'dynamic';
880
+ fixed: 'fixed';
881
881
  }>;
882
882
  /**
883
883
  * Pool type.
@@ -887,14 +887,14 @@ type PoolType = keyof typeof PoolTypes;
887
887
  * Enumeration of pool events.
888
888
  */
889
889
  declare const PoolEvents: Readonly<{
890
- ready: 'ready';
890
+ backPressure: 'backPressure';
891
891
  busy: 'busy';
892
- full: 'full';
893
- empty: 'empty';
894
892
  destroy: 'destroy';
893
+ empty: 'empty';
895
894
  error: 'error';
895
+ full: 'full';
896
+ ready: 'ready';
896
897
  taskError: 'taskError';
897
- backPressure: 'backPressure';
898
898
  }>;
899
899
  /**
900
900
  * Pool event.
@@ -904,84 +904,84 @@ type PoolEvent = keyof typeof PoolEvents;
904
904
  * Pool information.
905
905
  */
906
906
  interface PoolInfo {
907
- readonly version: string;
908
- readonly type: PoolType;
909
- readonly worker: WorkerType;
910
- readonly started: boolean;
911
- readonly ready: boolean;
912
- readonly defaultStrategy: WorkerChoiceStrategy;
913
- readonly strategyRetries: number;
914
- readonly minSize: number;
915
- readonly maxSize: number;
916
- /** Pool utilization. */
917
- readonly utilization?: number;
918
- /** Pool total worker nodes. */
919
- readonly workerNodes: number;
920
- /** Pool idle worker nodes. */
921
- readonly idleWorkerNodes: number;
922
- /** Pool busy worker nodes. */
923
- readonly busyWorkerNodes: number;
924
- /** Pool tasks stealing worker nodes. */
925
- readonly stealingWorkerNodes?: number;
907
+ readonly backPressure?: boolean;
926
908
  /** Pool tasks back pressure worker nodes. */
927
909
  readonly backPressureWorkerNodes?: number;
928
- readonly executedTasks: number;
929
- readonly executingTasks: number;
930
- readonly queuedTasks?: number;
931
- readonly maxQueuedTasks?: number;
932
- readonly backPressure?: boolean;
933
- readonly stolenTasks?: number;
934
- readonly failedTasks: number;
935
- readonly runTime?: {
936
- readonly minimum: number;
937
- readonly maximum: number;
938
- readonly average?: number;
939
- readonly median?: number;
940
- };
941
- readonly waitTime?: {
942
- readonly minimum: number;
943
- readonly maximum: number;
944
- readonly average?: number;
945
- readonly median?: number;
946
- };
910
+ /** Pool busy worker nodes. */
911
+ readonly busyWorkerNodes: number;
912
+ readonly defaultStrategy: WorkerChoiceStrategy;
947
913
  readonly elu?: {
948
- idle: {
949
- readonly minimum: number;
950
- readonly maximum: number;
914
+ active: {
951
915
  readonly average?: number;
916
+ readonly maximum: number;
952
917
  readonly median?: number;
953
- };
954
- active: {
955
918
  readonly minimum: number;
956
- readonly maximum: number;
919
+ };
920
+ idle: {
957
921
  readonly average?: number;
922
+ readonly maximum: number;
958
923
  readonly median?: number;
924
+ readonly minimum: number;
959
925
  };
960
926
  utilization: {
961
927
  readonly average?: number;
962
928
  readonly median?: number;
963
929
  };
964
930
  };
965
- }
966
- /**
967
- * Worker node tasks queue options.
968
- */
969
- interface TasksQueueOptions {
970
- /**
971
- * Maximum tasks queue size per worker node flagging it as back pressured.
972
- * @defaultValue (pool maximum size)^2
973
- */
974
- readonly size?: number;
975
- /**
976
- * Maximum number of tasks that can be executed concurrently on a worker node.
977
- * @defaultValue 1
978
- */
979
- readonly concurrency?: number;
980
- /**
981
- * Whether to enable task stealing on idle.
982
- * @defaultValue true
931
+ readonly executedTasks: number;
932
+ readonly executingTasks: number;
933
+ readonly failedTasks: number;
934
+ /** Pool idle worker nodes. */
935
+ readonly idleWorkerNodes: number;
936
+ readonly maxQueuedTasks?: number;
937
+ readonly maxSize: number;
938
+ readonly minSize: number;
939
+ readonly queuedTasks?: number;
940
+ readonly ready: boolean;
941
+ readonly runTime?: {
942
+ readonly average?: number;
943
+ readonly maximum: number;
944
+ readonly median?: number;
945
+ readonly minimum: number;
946
+ };
947
+ readonly started: boolean;
948
+ /** Pool tasks stealing worker nodes. */
949
+ readonly stealingWorkerNodes?: number;
950
+ readonly stolenTasks?: number;
951
+ readonly strategyRetries: number;
952
+ readonly type: PoolType;
953
+ /** Pool utilization. */
954
+ readonly utilization?: number;
955
+ readonly version: string;
956
+ readonly waitTime?: {
957
+ readonly average?: number;
958
+ readonly maximum: number;
959
+ readonly median?: number;
960
+ readonly minimum: number;
961
+ };
962
+ readonly worker: WorkerType;
963
+ /** Pool total worker nodes. */
964
+ readonly workerNodes: number;
965
+ }
966
+ /**
967
+ * Worker node tasks queue options.
968
+ */
969
+ interface TasksQueueOptions {
970
+ /**
971
+ * Maximum number of tasks that can be executed concurrently on a worker node.
972
+ * @defaultValue 1
983
973
  */
984
- readonly taskStealing?: boolean;
974
+ readonly concurrency?: number;
975
+ /**
976
+ * Maximum tasks queue size per worker node flagging it as back pressured.
977
+ * @defaultValue (pool maximum size)^2
978
+ */
979
+ readonly size?: number;
980
+ /**
981
+ * Queued tasks finished timeout in milliseconds at worker node termination.
982
+ * @defaultValue 2000
983
+ */
984
+ readonly tasksFinishedTimeout?: number;
985
985
  /**
986
986
  * Whether to enable tasks stealing under back pressure.
987
987
  * @defaultValue true
@@ -993,10 +993,10 @@ interface TasksQueueOptions {
993
993
  */
994
994
  readonly tasksStealingRatio?: number;
995
995
  /**
996
- * Queued tasks finished timeout in milliseconds at worker node termination.
997
- * @defaultValue 2000
996
+ * Whether to enable task stealing on idle.
997
+ * @defaultValue true
998
998
  */
999
- readonly tasksFinishedTimeout?: number;
999
+ readonly taskStealing?: boolean;
1000
1000
  }
1001
1001
  /**
1002
1002
  * Options for a poolifier pool.
@@ -1004,15 +1004,20 @@ interface TasksQueueOptions {
1004
1004
  */
1005
1005
  interface PoolOptions<Worker extends IWorker> {
1006
1006
  /**
1007
- * A function that will listen for online event on each worker.
1008
- * @defaultValue `() => {}`
1007
+ * Pool events integrated with async resource emission.
1008
+ * @defaultValue true
1009
1009
  */
1010
- onlineHandler?: OnlineHandler<Worker>;
1010
+ enableEvents?: boolean;
1011
1011
  /**
1012
- * A function that will listen for message event on each worker.
1013
- * @defaultValue `() => {}`
1012
+ * Pool worker node tasks queue.
1013
+ * @defaultValue false
1014
1014
  */
1015
- messageHandler?: MessageHandler<Worker>;
1015
+ enableTasksQueue?: boolean;
1016
+ /**
1017
+ * Key/value pairs to add to worker process environment.
1018
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1019
+ */
1020
+ env?: Record<string, unknown>;
1016
1021
  /**
1017
1022
  * A function that will listen for error event on each worker.
1018
1023
  * @defaultValue `() => {}`
@@ -1024,52 +1029,47 @@ interface PoolOptions<Worker extends IWorker> {
1024
1029
  */
1025
1030
  exitHandler?: ExitHandler<Worker>;
1026
1031
  /**
1027
- * Whether to start the minimum number of workers at pool initialization.
1028
- * @defaultValue true
1029
- */
1030
- startWorkers?: boolean;
1031
- /**
1032
- * The default worker choice strategy to use in this pool.
1033
- * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
1032
+ * A function that will listen for message event on each worker.
1033
+ * @defaultValue `() => {}`
1034
1034
  */
1035
- workerChoiceStrategy?: WorkerChoiceStrategy;
1035
+ messageHandler?: MessageHandler<Worker>;
1036
1036
  /**
1037
- * The worker choice strategy options.
1037
+ * A function that will listen for online event on each worker.
1038
+ * @defaultValue `() => {}`
1038
1039
  */
1039
- workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
1040
+ onlineHandler?: OnlineHandler<Worker>;
1040
1041
  /**
1041
1042
  * Restart worker on error.
1042
1043
  */
1043
1044
  restartWorkerOnError?: boolean;
1044
1045
  /**
1045
- * Pool events integrated with async resource emission.
1046
- * @defaultValue true
1046
+ * Cluster settings.
1047
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1047
1048
  */
1048
- enableEvents?: boolean;
1049
+ settings?: ClusterSettings;
1049
1050
  /**
1050
- * Pool worker node tasks queue.
1051
- * @defaultValue false
1051
+ * Whether to start the minimum number of workers at pool initialization.
1052
+ * @defaultValue true
1052
1053
  */
1053
- enableTasksQueue?: boolean;
1054
+ startWorkers?: boolean;
1054
1055
  /**
1055
1056
  * Pool worker node tasks queue options.
1056
1057
  */
1057
1058
  tasksQueueOptions?: TasksQueueOptions;
1058
1059
  /**
1059
- * Worker options.
1060
- * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1060
+ * The default worker choice strategy to use in this pool.
1061
+ * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
1061
1062
  */
1062
- workerOptions?: WorkerOptions$1;
1063
+ workerChoiceStrategy?: WorkerChoiceStrategy;
1063
1064
  /**
1064
- * Key/value pairs to add to worker process environment.
1065
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1065
+ * The worker choice strategy options.
1066
1066
  */
1067
- env?: Record<string, unknown>;
1067
+ workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
1068
1068
  /**
1069
- * Cluster settings.
1070
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1069
+ * Worker options.
1070
+ * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1071
1071
  */
1072
- settings?: ClusterSettings;
1072
+ workerOptions?: WorkerOptions$1;
1073
1073
  }
1074
1074
  /**
1075
1075
  * Contract definition for a poolifier pool.
@@ -1079,14 +1079,19 @@ interface PoolOptions<Worker extends IWorker> {
1079
1079
  */
1080
1080
  interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1081
1081
  /**
1082
- * Pool information.
1082
+ * Adds a task function to this pool.
1083
+ * If a task function with the same name already exists, it will be overwritten.
1084
+ * @param name - The name of the task function.
1085
+ * @param fn - The task function.
1086
+ * @returns `true` if the task function was added, `false` otherwise.
1087
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
1088
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function or task function object.
1083
1089
  */
1084
- readonly info: PoolInfo;
1090
+ readonly addTaskFunction: (name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>) => Promise<boolean>;
1085
1091
  /**
1086
- * Pool worker nodes.
1087
- * @internal
1092
+ * Terminates all workers in this pool.
1088
1093
  */
1089
- readonly workerNodes: IWorkerNode<Worker, Data>[];
1094
+ readonly destroy: () => Promise<void>;
1090
1095
  /**
1091
1096
  * Pool event emitter integrated with async resource.
1092
1097
  * The async tracking tooling identifier is `poolifier:<PoolType>-<WorkerType>-pool`.
@@ -1103,6 +1108,12 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1103
1108
  * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
1104
1109
  */
1105
1110
  readonly emitter?: EventEmitterAsyncResource;
1111
+ /**
1112
+ * Enables/disables the worker node tasks queue in this pool.
1113
+ * @param enable - Whether to enable or disable the worker node tasks queue.
1114
+ * @param tasksQueueOptions - The worker node tasks queue options.
1115
+ */
1116
+ readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
1106
1117
  /**
1107
1118
  * Executes the specified function in the worker constructor with the task data input parameter.
1108
1119
  * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
@@ -1111,22 +1122,6 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1111
1122
  * @returns Promise with a task function response that will be fulfilled when the task is completed.
1112
1123
  */
1113
1124
  readonly execute: (data?: Data, name?: string, transferList?: readonly TransferListItem[]) => Promise<Response>;
1114
- /**
1115
- * Executes the specified function in the worker constructor with the tasks data iterable input parameter.
1116
- * @param data - The tasks iterable input data for the specified task function. This can only be an iterable of structured-cloneable data.
1117
- * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
1118
- * @param transferList - An optional array of transferable objects to transfer ownership of. Ownership of the transferred objects is given to the chosen pool's worker_threads worker and they should not be used in the main thread afterwards.
1119
- * @returns Promise with an array of task function responses that will be fulfilled when the tasks are completed.
1120
- */
1121
- readonly mapExecute: (data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]) => Promise<Response[]>;
1122
- /**
1123
- * Starts the minimum number of workers in this pool.
1124
- */
1125
- readonly start: () => void;
1126
- /**
1127
- * Terminates all workers in this pool.
1128
- */
1129
- readonly destroy: () => Promise<void>;
1130
1125
  /**
1131
1126
  * Whether the specified task function exists in this pool.
1132
1127
  * @param name - The name of the task function.
@@ -1134,32 +1129,39 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1134
1129
  */
1135
1130
  readonly hasTaskFunction: (name: string) => boolean;
1136
1131
  /**
1137
- * Adds a task function to this pool.
1138
- * If a task function with the same name already exists, it will be overwritten.
1139
- * @param name - The name of the task function.
1140
- * @param fn - The task function.
1141
- * @returns `true` if the task function was added, `false` otherwise.
1142
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
1143
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function or task function object.
1132
+ * Pool information.
1144
1133
  */
1145
- readonly addTaskFunction: (name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>) => Promise<boolean>;
1134
+ readonly info: PoolInfo;
1135
+ /**
1136
+ * Lists the properties of task functions available in this pool.
1137
+ * @returns The properties of task functions available in this pool.
1138
+ */
1139
+ readonly listTaskFunctionsProperties: () => TaskFunctionProperties[];
1140
+ /**
1141
+ * Executes the specified function in the worker constructor with the tasks data iterable input parameter.
1142
+ * @param data - The tasks iterable input data for the specified task function. This can only be an iterable of structured-cloneable data.
1143
+ * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
1144
+ * @param transferList - An optional array of transferable objects to transfer ownership of. Ownership of the transferred objects is given to the chosen pool's worker_threads worker and they should not be used in the main thread afterwards.
1145
+ * @returns Promise with an array of task function responses that will be fulfilled when the tasks are completed.
1146
+ */
1147
+ readonly mapExecute: (data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]) => Promise<Response[]>;
1146
1148
  /**
1147
1149
  * Removes a task function from this pool.
1148
1150
  * @param name - The name of the task function.
1149
1151
  * @returns `true` if the task function was removed, `false` otherwise.
1150
1152
  */
1151
1153
  readonly removeTaskFunction: (name: string) => Promise<boolean>;
1152
- /**
1153
- * Lists the properties of task functions available in this pool.
1154
- * @returns The properties of task functions available in this pool.
1155
- */
1156
- readonly listTaskFunctionsProperties: () => TaskFunctionProperties[];
1157
1154
  /**
1158
1155
  * Sets the default task function in this pool.
1159
1156
  * @param name - The name of the task function.
1160
1157
  * @returns `true` if the default task function was set, `false` otherwise.
1161
1158
  */
1162
1159
  readonly setDefaultTaskFunction: (name: string) => Promise<boolean>;
1160
+ /**
1161
+ * Sets the worker node tasks queue options in this pool.
1162
+ * @param tasksQueueOptions - The worker node tasks queue options.
1163
+ */
1164
+ readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
1163
1165
  /**
1164
1166
  * Sets the default worker choice strategy in this pool.
1165
1167
  * @param workerChoiceStrategy - The default worker choice strategy.
@@ -1173,16 +1175,14 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1173
1175
  */
1174
1176
  readonly setWorkerChoiceStrategyOptions: (workerChoiceStrategyOptions: WorkerChoiceStrategyOptions) => boolean;
1175
1177
  /**
1176
- * Enables/disables the worker node tasks queue in this pool.
1177
- * @param enable - Whether to enable or disable the worker node tasks queue.
1178
- * @param tasksQueueOptions - The worker node tasks queue options.
1178
+ * Starts the minimum number of workers in this pool.
1179
1179
  */
1180
- readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
1180
+ readonly start: () => void;
1181
1181
  /**
1182
- * Sets the worker node tasks queue options in this pool.
1183
- * @param tasksQueueOptions - The worker node tasks queue options.
1182
+ * Pool worker nodes.
1183
+ * @internal
1184
1184
  */
1185
- readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
1185
+ readonly workerNodes: IWorkerNode<Worker, Data>[];
1186
1186
  }
1187
1187
 
1188
1188
  /**
@@ -1194,14 +1194,14 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1194
1194
  */
1195
1195
  declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unknown, Response = unknown> {
1196
1196
  private readonly pool;
1197
- /**
1198
- * The number of worker choice strategies execution retries.
1199
- */
1200
- retriesCount: number;
1201
1197
  /**
1202
1198
  * The default worker choice strategy in the context.
1203
1199
  */
1204
1200
  private defaultWorkerChoiceStrategy;
1201
+ /**
1202
+ * The maximum number of worker choice strategies execution retries.
1203
+ */
1204
+ private readonly retries;
1205
1205
  /**
1206
1206
  * The worker choice strategies registered in the context.
1207
1207
  */
@@ -1215,9 +1215,9 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1215
1215
  */
1216
1216
  private workerChoiceStrategiesTaskStatisticsRequirements;
1217
1217
  /**
1218
- * The maximum number of worker choice strategies execution retries.
1218
+ * The number of worker choice strategies execution retries.
1219
1219
  */
1220
- private readonly retries;
1220
+ retriesCount: number;
1221
1221
  /**
1222
1222
  * Worker choice strategies context constructor.
1223
1223
  * @param pool - The pool instance.
@@ -1225,6 +1225,34 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1225
1225
  * @param opts - The worker choice strategy options.
1226
1226
  */
1227
1227
  constructor(pool: IPool<Worker, Data, Response>, workerChoiceStrategies?: WorkerChoiceStrategy[], opts?: WorkerChoiceStrategyOptions);
1228
+ /**
1229
+ * Adds a worker choice strategy to the context.
1230
+ * @param workerChoiceStrategy - The worker choice strategy to add.
1231
+ * @param pool - The pool instance.
1232
+ * @param opts - The worker choice strategy options.
1233
+ * @returns The worker choice strategies.
1234
+ */
1235
+ private addWorkerChoiceStrategy;
1236
+ /**
1237
+ * Executes the given worker choice strategy.
1238
+ * @param workerChoiceStrategy - The worker choice strategy.
1239
+ * @returns The key of the worker node.
1240
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1241
+ */
1242
+ private executeStrategy;
1243
+ /**
1244
+ * Removes a worker choice strategy from the context.
1245
+ * @param workerChoiceStrategy - The worker choice strategy to remove.
1246
+ * @returns `true` if the worker choice strategy is removed, `false` otherwise.
1247
+ */
1248
+ private removeWorkerChoiceStrategy;
1249
+ /**
1250
+ * Executes the given worker choice strategy in the context algorithm.
1251
+ * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
1252
+ * @returns The key of the worker node.
1253
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1254
+ */
1255
+ execute(workerChoiceStrategy?: WorkerChoiceStrategy): number;
1228
1256
  /**
1229
1257
  * Gets the active worker choice strategies in the context policy.
1230
1258
  * @returns The strategies policy.
@@ -1235,6 +1263,12 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1235
1263
  * @returns The strategies task statistics requirements.
1236
1264
  */
1237
1265
  getTaskStatisticsRequirements(): TaskStatisticsRequirements;
1266
+ /**
1267
+ * Removes the worker node key from the active worker choice strategies in the context.
1268
+ * @param workerNodeKey - The worker node key.
1269
+ * @returns `true` if the removal is successful, `false` otherwise.
1270
+ */
1271
+ remove(workerNodeKey: number): boolean;
1238
1272
  /**
1239
1273
  * Sets the default worker choice strategy to use in the context.
1240
1274
  * @param workerChoiceStrategy - The default worker choice strategy to set.
@@ -1242,36 +1276,10 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1242
1276
  */
1243
1277
  setDefaultWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, opts?: WorkerChoiceStrategyOptions): void;
1244
1278
  /**
1245
- * Updates the worker node key in the active worker choice strategies in the context internals.
1246
- * @param workerNodeKey - The worker node key.
1247
- * @returns `true` if the update is successful, `false` otherwise.
1279
+ * Sets the active worker choice strategies in the context options.
1280
+ * @param opts - The worker choice strategy options.
1248
1281
  */
1249
- update(workerNodeKey: number): boolean;
1250
- /**
1251
- * Executes the given worker choice strategy in the context algorithm.
1252
- * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
1253
- * @returns The key of the worker node.
1254
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1255
- */
1256
- execute(workerChoiceStrategy?: WorkerChoiceStrategy): number;
1257
- /**
1258
- * Executes the given worker choice strategy.
1259
- * @param workerChoiceStrategy - The worker choice strategy.
1260
- * @returns The key of the worker node.
1261
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1262
- */
1263
- private executeStrategy;
1264
- /**
1265
- * Removes the worker node key from the active worker choice strategies in the context.
1266
- * @param workerNodeKey - The worker node key.
1267
- * @returns `true` if the removal is successful, `false` otherwise.
1268
- */
1269
- remove(workerNodeKey: number): boolean;
1270
- /**
1271
- * Sets the active worker choice strategies in the context options.
1272
- * @param opts - The worker choice strategy options.
1273
- */
1274
- setOptions(opts: WorkerChoiceStrategyOptions | undefined): void;
1282
+ setOptions(opts: undefined | WorkerChoiceStrategyOptions): void;
1275
1283
  /**
1276
1284
  * Synchronizes the active worker choice strategies in the context with the given worker choice strategies.
1277
1285
  * @param workerChoiceStrategies - The worker choice strategies to synchronize.
@@ -1279,19 +1287,11 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1279
1287
  */
1280
1288
  syncWorkerChoiceStrategies(workerChoiceStrategies: Set<WorkerChoiceStrategy>, opts?: WorkerChoiceStrategyOptions): void;
1281
1289
  /**
1282
- * Adds a worker choice strategy to the context.
1283
- * @param workerChoiceStrategy - The worker choice strategy to add.
1284
- * @param pool - The pool instance.
1285
- * @param opts - The worker choice strategy options.
1286
- * @returns The worker choice strategies.
1287
- */
1288
- private addWorkerChoiceStrategy;
1289
- /**
1290
- * Removes a worker choice strategy from the context.
1291
- * @param workerChoiceStrategy - The worker choice strategy to remove.
1292
- * @returns `true` if the worker choice strategy is removed, `false` otherwise.
1290
+ * Updates the worker node key in the active worker choice strategies in the context internals.
1291
+ * @param workerNodeKey - The worker node key.
1292
+ * @returns `true` if the update is successful, `false` otherwise.
1293
1293
  */
1294
- private removeWorkerChoiceStrategy;
1294
+ update(workerNodeKey: number): boolean;
1295
1295
  }
1296
1296
 
1297
1297
  /**
@@ -1305,10 +1305,6 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1305
1305
  protected readonly filePath: string;
1306
1306
  protected readonly opts: PoolOptions<Worker>;
1307
1307
  protected readonly maximumNumberOfWorkers?: number | undefined;
1308
- /** @inheritDoc */
1309
- readonly workerNodes: IWorkerNode<Worker, Data>[];
1310
- /** @inheritDoc */
1311
- emitter?: EventEmitterAsyncResource;
1312
1308
  /**
1313
1309
  * The task execution response promise map:
1314
1310
  * - `key`: The message id of each submitted task.
@@ -1322,35 +1318,73 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1322
1318
  */
1323
1319
  protected workerChoiceStrategiesContext?: WorkerChoiceStrategiesContext<Worker, Data, Response>;
1324
1320
  /**
1325
- * The task functions added at runtime map:
1326
- * - `key`: The task function name.
1327
- * - `value`: The task function object.
1321
+ * This method is the message listener registered on each worker.
1322
+ * @param message - The message received from the worker.
1328
1323
  */
1329
- private readonly taskFunctions;
1324
+ protected readonly workerMessageListener: (message: MessageValue<Response>) => void;
1330
1325
  /**
1331
- * Whether the pool is started or not.
1326
+ * Whether the pool is destroying or not.
1332
1327
  */
1333
- private started;
1328
+ private destroying;
1334
1329
  /**
1335
- * Whether the pool is starting or not.
1330
+ * Gets task function worker choice strategy, if any.
1331
+ * @param name - The task function name.
1332
+ * @returns The task function worker choice strategy if the task function worker choice strategy is defined, `undefined` otherwise.
1336
1333
  */
1337
- private starting;
1334
+ private readonly getTaskFunctionWorkerChoiceStrategy;
1338
1335
  /**
1339
- * Whether the pool is destroying or not.
1336
+ * Gets the worker choice strategies registered in this pool.
1337
+ * @returns The worker choice strategies.
1340
1338
  */
1341
- private destroying;
1339
+ private readonly getWorkerChoiceStrategies;
1342
1340
  /**
1343
- * Whether the minimum number of workers is starting or not.
1341
+ * Gets worker node task function priority, if any.
1342
+ * @param workerNodeKey - The worker node key.
1343
+ * @param name - The task function name.
1344
+ * @returns The worker node task function priority if the worker node task function priority is defined, `undefined` otherwise.
1344
1345
  */
1345
- private startingMinimumNumberOfWorkers;
1346
+ private readonly getWorkerNodeTaskFunctionPriority;
1347
+ /**
1348
+ * Gets worker node task function worker choice strategy, if any.
1349
+ * @param workerNodeKey - The worker node key.
1350
+ * @param name - The task function name.
1351
+ * @returns The worker node task function worker choice strategy if the worker node task function worker choice strategy is defined, `undefined` otherwise.
1352
+ */
1353
+ private readonly getWorkerNodeTaskFunctionWorkerChoiceStrategy;
1354
+ private readonly handleWorkerNodeBackPressureEvent;
1355
+ private readonly handleWorkerNodeIdleEvent;
1346
1356
  /**
1347
1357
  * Whether the pool ready event has been emitted or not.
1348
1358
  */
1349
1359
  private readyEventEmitted;
1360
+ /**
1361
+ * Whether the pool is started or not.
1362
+ */
1363
+ private started;
1364
+ /**
1365
+ * Whether the pool is starting or not.
1366
+ */
1367
+ private starting;
1368
+ /**
1369
+ * Whether the minimum number of workers is starting or not.
1370
+ */
1371
+ private startingMinimumNumberOfWorkers;
1350
1372
  /**
1351
1373
  * The start timestamp of the pool.
1352
1374
  */
1353
1375
  private startTimestamp?;
1376
+ private readonly stealTask;
1377
+ /**
1378
+ * The task functions added at runtime map:
1379
+ * - `key`: The task function name.
1380
+ * - `value`: The task function object.
1381
+ */
1382
+ private readonly taskFunctions;
1383
+ private readonly workerNodeStealTask;
1384
+ /** @inheritDoc */
1385
+ emitter?: EventEmitterAsyncResource;
1386
+ /** @inheritDoc */
1387
+ readonly workerNodes: IWorkerNode<Worker, Data>[];
1354
1388
  /**
1355
1389
  * Constructs a new poolifier pool.
1356
1390
  * @param minimumNumberOfWorkers - Minimum number of workers that this pool manages.
@@ -1359,169 +1393,126 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1359
1393
  * @param maximumNumberOfWorkers - Maximum number of workers that this pool manages.
1360
1394
  */
1361
1395
  constructor(minimumNumberOfWorkers: number, filePath: string, opts: PoolOptions<Worker>, maximumNumberOfWorkers?: number | undefined);
1362
- private checkPoolType;
1363
- private checkMinimumNumberOfWorkers;
1364
- private checkPoolOptions;
1365
- private checkValidWorkerChoiceStrategyOptions;
1366
- private initEventEmitter;
1367
- /** @inheritDoc */
1368
- get info(): PoolInfo;
1369
1396
  /**
1370
- * Whether the pool is ready or not.
1371
- * @returns The pool readiness boolean status.
1397
+ * Hook executed after the worker task execution.
1398
+ * Can be overridden.
1399
+ * @param workerNodeKey - The worker node key.
1400
+ * @param message - The received message.
1372
1401
  */
1373
- private get ready();
1402
+ protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
1374
1403
  /**
1375
- * Whether the pool is empty or not.
1376
- * @returns The pool emptiness boolean status.
1404
+ * Method hooked up after a worker node has been newly created.
1405
+ * Can be overridden.
1406
+ * @param workerNodeKey - The newly created worker node key.
1377
1407
  */
1378
- protected get empty(): boolean;
1408
+ protected afterWorkerNodeSetup(workerNodeKey: number): void;
1379
1409
  /**
1380
- * The approximate pool utilization.
1381
- * @returns The pool utilization.
1410
+ * Hook executed before the worker task execution.
1411
+ * Can be overridden.
1412
+ * @param workerNodeKey - The worker node key.
1413
+ * @param task - The task to execute.
1382
1414
  */
1383
- private get utilization();
1415
+ protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
1384
1416
  /**
1385
- * The pool type.
1386
- *
1387
- * If it is `'dynamic'`, it provides the `max` property.
1417
+ * Emits dynamic worker creation events.
1388
1418
  */
1389
- protected abstract get type(): PoolType;
1419
+ protected abstract checkAndEmitDynamicWorkerCreationEvents(): void;
1390
1420
  /**
1391
- * The worker type.
1421
+ * Creates a new, completely set up dynamic worker node.
1422
+ * @returns New, completely set up dynamic worker node key.
1392
1423
  */
1393
- protected abstract get worker(): WorkerType;
1424
+ protected createAndSetupDynamicWorkerNode(): number;
1394
1425
  /**
1395
- * Checks if the worker id sent in the received message from a worker is valid.
1396
- * @param message - The received message.
1397
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
1426
+ * Creates a new, completely set up worker node.
1427
+ * @returns New, completely set up worker node key.
1398
1428
  */
1399
- private checkMessageWorkerId;
1429
+ protected createAndSetupWorkerNode(): number;
1400
1430
  /**
1401
- * Gets the worker node key given its worker id.
1402
- * @param workerId - The worker id.
1403
- * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
1431
+ * Deregisters a listener callback on the worker given its worker node key.
1432
+ * @param workerNodeKey - The worker node key.
1433
+ * @param listener - The message listener callback.
1404
1434
  */
1405
- private getWorkerNodeKeyByWorkerId;
1406
- /** @inheritDoc */
1407
- setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions): void;
1408
- /** @inheritDoc */
1409
- setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | undefined): boolean;
1410
- /** @inheritDoc */
1411
- enableTasksQueue(enable: boolean, tasksQueueOptions?: TasksQueueOptions): void;
1412
- /** @inheritDoc */
1413
- setTasksQueueOptions(tasksQueueOptions: TasksQueueOptions | undefined): void;
1414
- private buildTasksQueueOptions;
1415
- private setTasksQueueSize;
1416
- private setTaskStealing;
1417
- private unsetTaskStealing;
1418
- private setTasksStealingOnBackPressure;
1419
- private unsetTasksStealingOnBackPressure;
1435
+ protected abstract deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1420
1436
  /**
1421
- * Whether the pool is full or not.
1422
- * @returns The pool fullness boolean status.
1437
+ * Terminates the worker node given its worker node key.
1438
+ * @param workerNodeKey - The worker node key.
1423
1439
  */
1424
- protected get full(): boolean;
1440
+ protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1441
+ protected flagWorkerNodeAsNotReady(workerNodeKey: number): void;
1442
+ protected flushTasksQueue(workerNodeKey: number): number;
1425
1443
  /**
1426
- * Whether the pool is busy or not.
1427
- * @returns The pool busyness boolean status.
1444
+ * Gets the worker information given its worker node key.
1445
+ * @param workerNodeKey - The worker node key.
1446
+ * @returns The worker information.
1428
1447
  */
1429
- protected abstract get busy(): boolean;
1448
+ protected getWorkerInfo(workerNodeKey: number): undefined | WorkerInfo;
1430
1449
  /**
1431
1450
  * Whether worker nodes are executing concurrently their tasks quota or not.
1432
1451
  * @returns Worker nodes busyness boolean status.
1433
1452
  */
1434
1453
  protected internalBusy(): boolean;
1435
- private isWorkerNodeIdle;
1436
- private isWorkerNodeBusy;
1437
- private sendTaskFunctionOperationToWorker;
1438
- private sendTaskFunctionOperationToWorkers;
1439
- /** @inheritDoc */
1440
- hasTaskFunction(name: string): boolean;
1441
- /** @inheritDoc */
1442
- addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): Promise<boolean>;
1443
- /** @inheritDoc */
1444
- removeTaskFunction(name: string): Promise<boolean>;
1445
- /** @inheritDoc */
1446
- listTaskFunctionsProperties(): TaskFunctionProperties[];
1447
1454
  /**
1448
- * Gets task function worker choice strategy, if any.
1449
- * @param name - The task function name.
1450
- * @returns The task function worker choice strategy if the task function worker choice strategy is defined, `undefined` otherwise.
1455
+ * Returns whether the worker is the main worker or not.
1456
+ * @returns `true` if the worker is the main worker, `false` otherwise.
1451
1457
  */
1452
- private readonly getTaskFunctionWorkerChoiceStrategy;
1458
+ protected abstract isMain(): boolean;
1453
1459
  /**
1454
- * Gets worker node task function worker choice strategy, if any.
1460
+ * Registers once a listener callback on the worker given its worker node key.
1455
1461
  * @param workerNodeKey - The worker node key.
1456
- * @param name - The task function name.
1457
- * @returns The worker node task function worker choice strategy if the worker node task function worker choice strategy is defined, `undefined` otherwise.
1462
+ * @param listener - The message listener callback.
1458
1463
  */
1459
- private readonly getWorkerNodeTaskFunctionWorkerChoiceStrategy;
1464
+ protected abstract registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1460
1465
  /**
1461
- * Gets worker node task function priority, if any.
1466
+ * Registers a listener callback on the worker given its worker node key.
1462
1467
  * @param workerNodeKey - The worker node key.
1463
- * @param name - The task function name.
1464
- * @returns The worker node task function priority if the worker node task function priority is defined, `undefined` otherwise.
1465
- */
1466
- private readonly getWorkerNodeTaskFunctionPriority;
1467
- /**
1468
- * Gets the worker choice strategies registered in this pool.
1469
- * @returns The worker choice strategies.
1468
+ * @param listener - The message listener callback.
1470
1469
  */
1471
- private readonly getWorkerChoiceStrategies;
1472
- /** @inheritDoc */
1473
- setDefaultTaskFunction(name: string): Promise<boolean>;
1474
- private shallExecuteTask;
1475
- private internalExecute;
1476
- /** @inheritDoc */
1477
- execute(data?: Data, name?: string, transferList?: readonly TransferListItem[]): Promise<Response>;
1478
- /** @inheritDoc */
1479
- mapExecute(data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]): Promise<Response[]>;
1470
+ protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1480
1471
  /**
1481
- * Starts the minimum number of workers.
1482
- * @param initWorkerNodeUsage - Whether to initialize the worker node usage or not. @defaultValue false
1472
+ * Sends the startup message to worker given its worker node key.
1473
+ * @param workerNodeKey - The worker node key.
1483
1474
  */
1484
- private startMinimumNumberOfWorkers;
1485
- /** @inheritdoc */
1486
- start(): void;
1487
- /** @inheritDoc */
1488
- destroy(): Promise<void>;
1489
- private sendKillMessageToWorker;
1475
+ protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
1490
1476
  /**
1491
- * Terminates the worker node given its worker node key.
1477
+ * Sends a message to worker given its worker node key.
1492
1478
  * @param workerNodeKey - The worker node key.
1479
+ * @param message - The message.
1480
+ * @param transferList - The optional array of transferable objects.
1493
1481
  */
1494
- protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1482
+ protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1495
1483
  /**
1496
1484
  * Setup hook to execute code before worker nodes are created in the abstract constructor.
1497
1485
  * Can be overridden.
1498
1486
  */
1499
1487
  protected setupHook(): void;
1500
1488
  /**
1501
- * Returns whether the worker is the main worker or not.
1502
- * @returns `true` if the worker is the main worker, `false` otherwise.
1489
+ * Conditions for dynamic worker creation.
1490
+ * @returns Whether to create a dynamic worker or not.
1503
1491
  */
1504
- protected abstract isMain(): boolean;
1492
+ protected abstract shallCreateDynamicWorker(): boolean;
1505
1493
  /**
1506
- * Hook executed before the worker task execution.
1507
- * Can be overridden.
1508
- * @param workerNodeKey - The worker node key.
1509
- * @param task - The task to execute.
1494
+ * Adds the given worker node in the pool worker nodes.
1495
+ * @param workerNode - The worker node.
1496
+ * @returns The added worker node key.
1497
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1510
1498
  */
1511
- protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
1499
+ private addWorkerNode;
1500
+ private buildTasksQueueOptions;
1501
+ private cannotStealTask;
1502
+ private checkAndEmitEmptyEvent;
1503
+ private checkAndEmitReadyEvent;
1504
+ private checkAndEmitTaskExecutionEvents;
1505
+ private checkAndEmitTaskQueuingEvents;
1512
1506
  /**
1513
- * Hook executed after the worker task execution.
1514
- * Can be overridden.
1515
- * @param workerNodeKey - The worker node key.
1507
+ * Checks if the worker id sent in the received message from a worker is valid.
1516
1508
  * @param message - The received message.
1509
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
1517
1510
  */
1518
- protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
1519
- /**
1520
- * Whether the worker node shall update its task function worker usage or not.
1521
- * @param workerNodeKey - The worker node key.
1522
- * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
1523
- */
1524
- private shallUpdateTaskFunctionWorkerUsage;
1511
+ private checkMessageWorkerId;
1512
+ private checkMinimumNumberOfWorkers;
1513
+ private checkPoolOptions;
1514
+ private checkPoolType;
1515
+ private checkValidWorkerChoiceStrategyOptions;
1525
1516
  /**
1526
1517
  * Chooses a worker node for the next task.
1527
1518
  * @param name - The task function name.
@@ -1529,129 +1520,138 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1529
1520
  */
1530
1521
  private chooseWorkerNode;
1531
1522
  /**
1532
- * Conditions for dynamic worker creation.
1533
- * @returns Whether to create a dynamic worker or not.
1523
+ * Creates a worker node.
1524
+ * @returns The created worker node.
1534
1525
  */
1535
- protected abstract shallCreateDynamicWorker(): boolean;
1526
+ private createWorkerNode;
1527
+ private dequeueTask;
1528
+ private enqueueTask;
1536
1529
  /**
1537
- * Sends a message to worker given its worker node key.
1530
+ * Executes the given task on the worker given its worker node key.
1538
1531
  * @param workerNodeKey - The worker node key.
1539
- * @param message - The message.
1540
- * @param transferList - The optional array of transferable objects.
1532
+ * @param task - The task to execute.
1541
1533
  */
1542
- protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1534
+ private executeTask;
1535
+ private flushTasksQueues;
1536
+ private getTasksQueuePriority;
1537
+ /**
1538
+ * Gets the worker node key given its worker id.
1539
+ * @param workerId - The worker id.
1540
+ * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
1541
+ */
1542
+ private getWorkerNodeKeyByWorkerId;
1543
+ private handleTask;
1544
+ private handleTaskExecutionResponse;
1545
+ private handleWorkerReadyResponse;
1546
+ private hasBackPressure;
1547
+ private initEventEmitter;
1543
1548
  /**
1544
1549
  * Initializes the worker node usage with sensible default values gathered during runtime.
1545
1550
  * @param workerNode - The worker node.
1546
1551
  */
1547
1552
  private initWorkerNodeUsage;
1553
+ private internalExecute;
1554
+ private isWorkerNodeBusy;
1555
+ private isWorkerNodeIdle;
1556
+ private redistributeQueuedTasks;
1548
1557
  /**
1549
- * Creates a new, completely set up worker node.
1550
- * @returns New, completely set up worker node key.
1551
- */
1552
- protected createAndSetupWorkerNode(): number;
1553
- /**
1554
- * Creates a new, completely set up dynamic worker node.
1555
- * @returns New, completely set up dynamic worker node key.
1556
- */
1557
- protected createAndSetupDynamicWorkerNode(): number;
1558
- /**
1559
- * Registers a listener callback on the worker given its worker node key.
1560
- * @param workerNodeKey - The worker node key.
1561
- * @param listener - The message listener callback.
1562
- */
1563
- protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1564
- /**
1565
- * Registers once a listener callback on the worker given its worker node key.
1566
- * @param workerNodeKey - The worker node key.
1567
- * @param listener - The message listener callback.
1558
+ * Removes the worker node from the pool worker nodes.
1559
+ * @param workerNode - The worker node.
1568
1560
  */
1569
- protected abstract registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1561
+ private removeWorkerNode;
1562
+ private resetTaskSequentiallyStolenStatisticsWorkerUsage;
1563
+ private sendKillMessageToWorker;
1570
1564
  /**
1571
- * Deregisters a listener callback on the worker given its worker node key.
1565
+ * Sends the statistics message to worker given its worker node key.
1572
1566
  * @param workerNodeKey - The worker node key.
1573
- * @param listener - The message listener callback.
1574
- */
1575
- protected abstract deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1576
- /**
1577
- * Method hooked up after a worker node has been newly created.
1578
- * Can be overridden.
1579
- * @param workerNodeKey - The newly created worker node key.
1580
1567
  */
1581
- protected afterWorkerNodeSetup(workerNodeKey: number): void;
1568
+ private sendStatisticsMessageToWorker;
1569
+ private sendTaskFunctionOperationToWorker;
1570
+ private sendTaskFunctionOperationToWorkers;
1571
+ private setTasksQueuePriority;
1572
+ private setTasksQueueSize;
1573
+ private setTasksStealingOnBackPressure;
1574
+ private setTaskStealing;
1575
+ private shallExecuteTask;
1582
1576
  /**
1583
- * Sends the startup message to worker given its worker node key.
1577
+ * Whether the worker node shall update its task function worker usage or not.
1584
1578
  * @param workerNodeKey - The worker node key.
1579
+ * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
1585
1580
  */
1586
- protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
1581
+ private shallUpdateTaskFunctionWorkerUsage;
1587
1582
  /**
1588
- * Sends the statistics message to worker given its worker node key.
1589
- * @param workerNodeKey - The worker node key.
1583
+ * Starts the minimum number of workers.
1584
+ * @param initWorkerNodeUsage - Whether to initialize the worker node usage or not. @defaultValue false
1590
1585
  */
1591
- private sendStatisticsMessageToWorker;
1592
- private cannotStealTask;
1593
- private handleTask;
1594
- private redistributeQueuedTasks;
1595
- private updateTaskStolenStatisticsWorkerUsage;
1586
+ private startMinimumNumberOfWorkers;
1587
+ private tasksQueueSize;
1588
+ private unsetTasksStealingOnBackPressure;
1589
+ private unsetTaskStealing;
1596
1590
  private updateTaskSequentiallyStolenStatisticsWorkerUsage;
1597
- private resetTaskSequentiallyStolenStatisticsWorkerUsage;
1598
- private readonly stealTask;
1599
- private readonly handleWorkerNodeIdleEvent;
1600
- private readonly workerNodeStealTask;
1601
- private readonly handleWorkerNodeBackPressureEvent;
1602
- private setTasksQueuePriority;
1591
+ private updateTaskStolenStatisticsWorkerUsage;
1592
+ /** @inheritDoc */
1593
+ addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): Promise<boolean>;
1594
+ /** @inheritDoc */
1595
+ destroy(): Promise<void>;
1596
+ /** @inheritDoc */
1597
+ enableTasksQueue(enable: boolean, tasksQueueOptions?: TasksQueueOptions): void;
1598
+ /** @inheritDoc */
1599
+ execute(data?: Data, name?: string, transferList?: readonly TransferListItem[]): Promise<Response>;
1600
+ /** @inheritDoc */
1601
+ hasTaskFunction(name: string): boolean;
1602
+ /** @inheritDoc */
1603
+ listTaskFunctionsProperties(): TaskFunctionProperties[];
1604
+ /** @inheritDoc */
1605
+ mapExecute(data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]): Promise<Response[]>;
1606
+ /** @inheritDoc */
1607
+ removeTaskFunction(name: string): Promise<boolean>;
1608
+ /** @inheritDoc */
1609
+ setDefaultTaskFunction(name: string): Promise<boolean>;
1610
+ /** @inheritDoc */
1611
+ setTasksQueueOptions(tasksQueueOptions: TasksQueueOptions | undefined): void;
1612
+ /** @inheritDoc */
1613
+ setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions): void;
1614
+ /** @inheritDoc */
1615
+ setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions: undefined | WorkerChoiceStrategyOptions): boolean;
1616
+ /** @inheritdoc */
1617
+ start(): void;
1603
1618
  /**
1604
- * This method is the message listener registered on each worker.
1605
- * @param message - The message received from the worker.
1619
+ * Whether the pool is busy or not.
1620
+ * @returns The pool busyness boolean status.
1606
1621
  */
1607
- protected readonly workerMessageListener: (message: MessageValue<Response>) => void;
1608
- private checkAndEmitReadyEvent;
1609
- private handleWorkerReadyResponse;
1610
- private handleTaskExecutionResponse;
1611
- private checkAndEmitTaskExecutionEvents;
1612
- private checkAndEmitTaskQueuingEvents;
1622
+ protected abstract get busy(): boolean;
1613
1623
  /**
1614
- * Emits dynamic worker creation events.
1624
+ * Whether the pool is empty or not.
1625
+ * @returns The pool emptiness boolean status.
1615
1626
  */
1616
- protected abstract checkAndEmitDynamicWorkerCreationEvents(): void;
1627
+ protected get empty(): boolean;
1617
1628
  /**
1618
- * Gets the worker information given its worker node key.
1619
- * @param workerNodeKey - The worker node key.
1620
- * @returns The worker information.
1629
+ * Whether the pool is full or not.
1630
+ * @returns The pool fullness boolean status.
1621
1631
  */
1622
- protected getWorkerInfo(workerNodeKey: number): WorkerInfo | undefined;
1623
- private getTasksQueuePriority;
1632
+ protected get full(): boolean;
1633
+ /** @inheritDoc */
1634
+ get info(): PoolInfo;
1624
1635
  /**
1625
- * Creates a worker node.
1626
- * @returns The created worker node.
1636
+ * Whether the pool is ready or not.
1637
+ * @returns The pool readiness boolean status.
1627
1638
  */
1628
- private createWorkerNode;
1639
+ private get ready();
1629
1640
  /**
1630
- * Adds the given worker node in the pool worker nodes.
1631
- * @param workerNode - The worker node.
1632
- * @returns The added worker node key.
1633
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1641
+ * The pool type.
1642
+ *
1643
+ * If it is `'dynamic'`, it provides the `max` property.
1634
1644
  */
1635
- private addWorkerNode;
1636
- private checkAndEmitEmptyEvent;
1645
+ protected abstract get type(): PoolType;
1637
1646
  /**
1638
- * Removes the worker node from the pool worker nodes.
1639
- * @param workerNode - The worker node.
1647
+ * The approximate pool utilization.
1648
+ * @returns The pool utilization.
1640
1649
  */
1641
- private removeWorkerNode;
1642
- protected flagWorkerNodeAsNotReady(workerNodeKey: number): void;
1643
- private hasBackPressure;
1650
+ private get utilization();
1644
1651
  /**
1645
- * Executes the given task on the worker given its worker node key.
1646
- * @param workerNodeKey - The worker node key.
1647
- * @param task - The task to execute.
1652
+ * The worker type.
1648
1653
  */
1649
- private executeTask;
1650
- private enqueueTask;
1651
- private dequeueTask;
1652
- private tasksQueueSize;
1653
- protected flushTasksQueue(workerNodeKey: number): number;
1654
- private flushTasksQueues;
1654
+ protected abstract get worker(): WorkerType;
1655
1655
  }
1656
1656
 
1657
1657
  /**
@@ -1675,29 +1675,29 @@ declare class FixedClusterPool<Data = unknown, Response = unknown> extends Abstr
1675
1675
  */
1676
1676
  constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions, maximumNumberOfWorkers?: number);
1677
1677
  /** @inheritDoc */
1678
- protected setupHook(): void;
1678
+ protected checkAndEmitDynamicWorkerCreationEvents(): void;
1679
1679
  /** @inheritDoc */
1680
- protected isMain(): boolean;
1680
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1681
1681
  /** @inheritDoc */
1682
- protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
1682
+ protected isMain(): boolean;
1683
1683
  /** @inheritDoc */
1684
- protected sendStartupMessageToWorker(workerNodeKey: number): void;
1684
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1685
1685
  /** @inheritDoc */
1686
1686
  protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1687
1687
  /** @inheritDoc */
1688
- protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1688
+ protected sendStartupMessageToWorker(workerNodeKey: number): void;
1689
1689
  /** @inheritDoc */
1690
- protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1690
+ protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
1691
+ /** @inheritDoc */
1692
+ protected setupHook(): void;
1691
1693
  /** @inheritDoc */
1692
1694
  protected shallCreateDynamicWorker(): boolean;
1693
1695
  /** @inheritDoc */
1694
- protected checkAndEmitDynamicWorkerCreationEvents(): void;
1696
+ protected get busy(): boolean;
1695
1697
  /** @inheritDoc */
1696
1698
  protected get type(): PoolType;
1697
1699
  /** @inheritDoc */
1698
1700
  protected get worker(): WorkerType;
1699
- /** @inheritDoc */
1700
- protected get busy(): boolean;
1701
1701
  }
1702
1702
 
1703
1703
  /**
@@ -1720,13 +1720,13 @@ declare class DynamicClusterPool<Data = unknown, Response = unknown> extends Fix
1720
1720
  */
1721
1721
  constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
1722
1722
  /** @inheritDoc */
1723
- protected shallCreateDynamicWorker(): boolean;
1724
- /** @inheritDoc */
1725
1723
  protected checkAndEmitDynamicWorkerCreationEvents(): void;
1726
1724
  /** @inheritDoc */
1727
- protected get type(): PoolType;
1725
+ protected shallCreateDynamicWorker(): boolean;
1728
1726
  /** @inheritDoc */
1729
1727
  protected get busy(): boolean;
1728
+ /** @inheritDoc */
1729
+ protected get type(): PoolType;
1730
1730
  }
1731
1731
 
1732
1732
  /**
@@ -1750,27 +1750,27 @@ declare class FixedThreadPool<Data = unknown, Response = unknown> extends Abstra
1750
1750
  */
1751
1751
  constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions, maximumNumberOfThreads?: number);
1752
1752
  /** @inheritDoc */
1753
- protected isMain(): boolean;
1753
+ protected checkAndEmitDynamicWorkerCreationEvents(): void;
1754
1754
  /** @inheritDoc */
1755
- protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1755
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1756
1756
  /** @inheritDoc */
1757
- protected sendStartupMessageToWorker(workerNodeKey: number): void;
1757
+ protected isMain(): boolean;
1758
+ /** @inheritDoc */
1759
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1758
1760
  /** @inheritDoc */
1759
1761
  protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1760
1762
  /** @inheritDoc */
1761
- protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1763
+ protected sendStartupMessageToWorker(workerNodeKey: number): void;
1762
1764
  /** @inheritDoc */
1763
- protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1765
+ protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1764
1766
  /** @inheritDoc */
1765
1767
  protected shallCreateDynamicWorker(): boolean;
1766
1768
  /** @inheritDoc */
1767
- protected checkAndEmitDynamicWorkerCreationEvents(): void;
1769
+ protected get busy(): boolean;
1768
1770
  /** @inheritDoc */
1769
1771
  protected get type(): PoolType;
1770
1772
  /** @inheritDoc */
1771
1773
  protected get worker(): WorkerType;
1772
- /** @inheritDoc */
1773
- protected get busy(): boolean;
1774
1774
  }
1775
1775
 
1776
1776
  /**
@@ -1793,13 +1793,13 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1793
1793
  */
1794
1794
  constructor(min: number, max: number, filePath: string, opts?: ThreadPoolOptions);
1795
1795
  /** @inheritDoc */
1796
- protected shallCreateDynamicWorker(): boolean;
1797
- /** @inheritDoc */
1798
1796
  protected checkAndEmitDynamicWorkerCreationEvents(): void;
1799
1797
  /** @inheritDoc */
1800
- protected get type(): PoolType;
1798
+ protected shallCreateDynamicWorker(): boolean;
1801
1799
  /** @inheritDoc */
1802
1800
  protected get busy(): boolean;
1801
+ /** @inheritDoc */
1802
+ protected get type(): PoolType;
1803
1803
  }
1804
1804
 
1805
1805
  /**
@@ -1808,10 +1808,10 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1808
1808
  * @internal
1809
1809
  */
1810
1810
  declare class PriorityQueue<T> {
1811
- private head;
1812
- private tail;
1813
1811
  private readonly bucketSize;
1812
+ private head;
1814
1813
  private priorityEnabled;
1814
+ private tail;
1815
1815
  /** The priority queue maximum size. */
1816
1816
  maxSize: number;
1817
1817
  /**
@@ -1821,33 +1821,11 @@ declare class PriorityQueue<T> {
1821
1821
  * @returns PriorityQueue.
1822
1822
  */
1823
1823
  constructor(bucketSize?: number, enablePriority?: boolean);
1824
+ private getPriorityQueueNode;
1824
1825
  /**
1825
- * The priority queue size.
1826
- * @returns The priority queue size.
1827
- */
1828
- get size(): number;
1829
- /**
1830
- * Whether priority is enabled.
1831
- * @returns Whether priority is enabled.
1832
- */
1833
- get enablePriority(): boolean;
1834
- /**
1835
- * Enables/disables priority.
1836
- * @param enablePriority - Whether to enable priority.
1837
- */
1838
- set enablePriority(enablePriority: boolean);
1839
- /**
1840
- * The number of filled prioritized buckets.
1841
- * @returns The number of filled prioritized buckets.
1842
- */
1843
- get buckets(): number;
1844
- /**
1845
- * Enqueue data into the priority queue.
1846
- * @param data - Data to enqueue.
1847
- * @param priority - Priority of the data. Lower values have higher priority.
1848
- * @returns The new size of the priority queue.
1826
+ * Clears the priority queue.
1849
1827
  */
1850
- enqueue(data: T, priority?: number): number;
1828
+ clear(): void;
1851
1829
  /**
1852
1830
  * Dequeue data from the priority queue.
1853
1831
  * @param bucket - The prioritized bucket to dequeue from.
@@ -1855,16 +1833,38 @@ declare class PriorityQueue<T> {
1855
1833
  */
1856
1834
  dequeue(bucket?: number): T | undefined;
1857
1835
  /**
1858
- * Clears the priority queue.
1836
+ * Enqueue data into the priority queue.
1837
+ * @param data - Data to enqueue.
1838
+ * @param priority - Priority of the data. Lower values have higher priority.
1839
+ * @returns The new size of the priority queue.
1859
1840
  */
1860
- clear(): void;
1841
+ enqueue(data: T, priority?: number): number;
1861
1842
  /**
1862
1843
  * Returns an iterator for the priority queue.
1863
1844
  * @returns An iterator for the priority queue.
1864
1845
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
1865
1846
  */
1866
1847
  [Symbol.iterator](): Iterator<T>;
1867
- private getPriorityQueueNode;
1848
+ /**
1849
+ * The number of filled prioritized buckets.
1850
+ * @returns The number of filled prioritized buckets.
1851
+ */
1852
+ get buckets(): number;
1853
+ /**
1854
+ * Whether priority is enabled.
1855
+ * @returns Whether priority is enabled.
1856
+ */
1857
+ get enablePriority(): boolean;
1858
+ /**
1859
+ * Enables/disables priority.
1860
+ * @param enablePriority - Whether to enable priority.
1861
+ */
1862
+ set enablePriority(enablePriority: boolean);
1863
+ /**
1864
+ * The priority queue size.
1865
+ * @returns The priority queue size.
1866
+ */
1867
+ get size(): number;
1868
1868
  }
1869
1869
 
1870
1870
  /**
@@ -1882,22 +1882,28 @@ interface FixedQueueNode<T> {
1882
1882
  * @internal
1883
1883
  */
1884
1884
  interface IFixedQueue<T> {
1885
+ /**
1886
+ * Returns an iterator for the fixed queue.
1887
+ * @returns An iterator for the fixed queue.
1888
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
1889
+ */
1890
+ [Symbol.iterator]: () => Iterator<T>;
1885
1891
  /** The fixed queue capacity. */
1886
1892
  readonly capacity: number;
1887
- /** The fixed queue size. */
1888
- readonly size: number;
1889
- /** The fixed queue node array. */
1890
- nodeArray: FixedQueueNode<T>[];
1893
+ /**
1894
+ * Clears the fixed queue.
1895
+ */
1896
+ clear: () => void;
1897
+ /**
1898
+ * Dequeue data from the fixed queue.
1899
+ * @returns The dequeued data or `undefined` if the fixed queue is empty.
1900
+ */
1901
+ dequeue: () => T | undefined;
1891
1902
  /**
1892
1903
  * Checks if the fixed queue is empty.
1893
1904
  * @returns `true` if the fixed queue is empty, `false` otherwise.
1894
1905
  */
1895
1906
  empty: () => boolean;
1896
- /**
1897
- * Checks if the fixed queue is full.
1898
- * @returns `true` if the fixed queue is full, `false` otherwise.
1899
- */
1900
- full: () => boolean;
1901
1907
  /**
1902
1908
  * Enqueue data into the fixed queue.
1903
1909
  * @param data - Data to enqueue.
@@ -1906,27 +1912,21 @@ interface IFixedQueue<T> {
1906
1912
  * @throws If the fixed queue is full.
1907
1913
  */
1908
1914
  enqueue: (data: T, priority?: number) => number;
1915
+ /**
1916
+ * Checks if the fixed queue is full.
1917
+ * @returns `true` if the fixed queue is full, `false` otherwise.
1918
+ */
1919
+ full: () => boolean;
1909
1920
  /**
1910
1921
  * Gets data from the fixed queue.
1911
1922
  * @param index - The index of the data to get.
1912
1923
  * @returns The data at the index or `undefined` if the fixed queue is empty or the index is out of bounds.
1913
1924
  */
1914
1925
  get: (index: number) => T | undefined;
1915
- /**
1916
- * Dequeue data from the fixed queue.
1917
- * @returns The dequeued data or `undefined` if the fixed queue is empty.
1918
- */
1919
- dequeue: () => T | undefined;
1920
- /**
1921
- * Clears the fixed queue.
1922
- */
1923
- clear: () => void;
1924
- /**
1925
- * Returns an iterator for the fixed queue.
1926
- * @returns An iterator for the fixed queue.
1927
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
1928
- */
1929
- [Symbol.iterator]: () => Iterator<T>;
1926
+ /** The fixed queue node array. */
1927
+ nodeArray: FixedQueueNode<T>[];
1928
+ /** The fixed queue size. */
1929
+ readonly size: number;
1930
1930
  }
1931
1931
 
1932
1932
  /**
@@ -1942,30 +1942,47 @@ declare const availableParallelism: () => number;
1942
1942
  * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
1943
1943
  * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
1944
1944
  */
1945
- declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> {
1945
+ declare abstract class AbstractWorker<MainWorker extends MessagePort | Worker, Data = unknown, Response = unknown> {
1946
1946
  protected readonly isMain: boolean | undefined;
1947
1947
  private readonly mainWorker;
1948
1948
  protected opts: WorkerOptions;
1949
1949
  /**
1950
- * Worker id.
1950
+ * Handler id of the `activeInterval` worker activity check.
1951
1951
  */
1952
- protected abstract id: number;
1952
+ protected activeInterval?: NodeJS.Timeout;
1953
1953
  /**
1954
- * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
1954
+ * Worker id.
1955
1955
  */
1956
- protected taskFunctions: Map<string, TaskFunctionObject<Data, Response>>;
1956
+ protected abstract id: number;
1957
1957
  /**
1958
1958
  * Timestamp of the last task processed by this worker.
1959
1959
  */
1960
1960
  protected lastTaskTimestamp: number;
1961
+ /**
1962
+ * Runs the given task.
1963
+ * @param task - The task to execute.
1964
+ */
1965
+ protected readonly run: (task: Task<Data>) => void;
1966
+ /**
1967
+ * Runs the given task function asynchronously.
1968
+ * @param fn - Task function that will be executed.
1969
+ * @param task - Input data for the task function.
1970
+ */
1971
+ protected readonly runAsync: (fn: TaskAsyncFunction<Data, Response>, task: Task<Data>) => void;
1972
+ /**
1973
+ * Runs the given task function synchronously.
1974
+ * @param fn - Task function that will be executed.
1975
+ * @param task - Input data for the task function.
1976
+ */
1977
+ protected readonly runSync: (fn: TaskSyncFunction<Data, Response>, task: Task<Data>) => void;
1961
1978
  /**
1962
1979
  * Performance statistics computation requirements.
1963
1980
  */
1964
1981
  protected statistics?: WorkerStatistics;
1965
1982
  /**
1966
- * Handler id of the `activeInterval` worker activity check.
1983
+ * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
1967
1984
  */
1968
- protected activeInterval?: NodeJS.Timeout;
1985
+ protected taskFunctions: Map<string, TaskFunctionObject<Data, Response>>;
1969
1986
  /**
1970
1987
  * Constructs a new poolifier worker.
1971
1988
  * @param isMain - Whether this is the main worker or not.
@@ -1973,66 +1990,62 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
1973
1990
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
1974
1991
  * @param opts - Options for the worker.
1975
1992
  */
1976
- constructor(isMain: boolean | undefined, mainWorker: MainWorker | undefined | null, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1977
- private checkWorkerOptions;
1978
- /**
1979
- * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
1980
- * @param taskFunctions - The task function(s) parameter that should be checked.
1981
- */
1982
- private checkTaskFunctions;
1983
- /**
1984
- * Checks if the worker has a task function with the given name.
1985
- * @param name - The name of the task function to check.
1986
- * @returns Whether the worker has a task function with the given name or not.
1987
- */
1988
- hasTaskFunction(name: string): TaskFunctionOperationResult;
1989
- /**
1990
- * Adds a task function to the worker.
1991
- * If a task function with the same name already exists, it is replaced.
1992
- * @param name - The name of the task function to add.
1993
- * @param fn - The task function to add.
1994
- * @returns Whether the task function was added or not.
1995
- */
1996
- addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): TaskFunctionOperationResult;
1993
+ constructor(isMain: boolean | undefined, mainWorker: MainWorker | null | undefined, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1997
1994
  /**
1998
- * Removes a task function from the worker.
1999
- * @param name - The name of the task function to remove.
2000
- * @returns Whether the task function existed and was removed or not.
1995
+ * Returns the main worker.
1996
+ * @returns Reference to the main worker.
1997
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
2001
1998
  */
2002
- removeTaskFunction(name: string): TaskFunctionOperationResult;
1999
+ protected getMainWorker(): MainWorker;
2003
2000
  /**
2004
- * Lists the properties of the worker's task functions.
2005
- * @returns The properties of the worker's task functions.
2001
+ * Handles an error and convert it to a string so it can be sent back to the main worker.
2002
+ * @param error - The error raised by the worker.
2003
+ * @returns The error message.
2006
2004
  */
2007
- listTaskFunctionsProperties(): TaskFunctionProperties[];
2005
+ protected handleError(error: Error | string): string;
2008
2006
  /**
2009
- * Sets the default task function to use in the worker.
2010
- * @param name - The name of the task function to use as default task function.
2011
- * @returns Whether the default task function was set or not.
2007
+ * Handles a kill message sent by the main worker.
2008
+ * @param message - The kill message.
2012
2009
  */
2013
- setDefaultTaskFunction(name: string): TaskFunctionOperationResult;
2010
+ protected handleKillMessage(message: MessageValue<Data>): void;
2014
2011
  /**
2015
2012
  * Handles the ready message sent by the main worker.
2016
2013
  * @param message - The ready message.
2017
2014
  */
2018
2015
  protected abstract handleReadyMessage(message: MessageValue<Data>): void;
2016
+ protected handleTaskFunctionOperationMessage(message: MessageValue<Data>): void;
2019
2017
  /**
2020
2018
  * Worker message listener.
2021
2019
  * @param message - The received message.
2022
2020
  */
2023
2021
  protected messageListener(message: MessageValue<Data>): void;
2024
- protected handleTaskFunctionOperationMessage(message: MessageValue<Data>): void;
2025
2022
  /**
2026
- * Handles a kill message sent by the main worker.
2027
- * @param message - The kill message.
2023
+ * Sends task functions properties to the main worker.
2028
2024
  */
2029
- protected handleKillMessage(message: MessageValue<Data>): void;
2025
+ protected sendTaskFunctionsPropertiesToMainWorker(): void;
2026
+ /**
2027
+ * Sends a message to main worker.
2028
+ * @param message - The response message.
2029
+ */
2030
+ protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
2031
+ private beginTaskPerformance;
2032
+ /**
2033
+ * Checks if the worker should be terminated, because its living too long.
2034
+ */
2035
+ private checkActive;
2030
2036
  /**
2031
2037
  * Check if the message worker id is set and matches the worker id.
2032
2038
  * @param message - The message to check.
2033
2039
  * @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
2034
2040
  */
2035
2041
  private checkMessageWorkerId;
2042
+ /**
2043
+ * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
2044
+ * @param taskFunctions - The task function(s) parameter that should be checked.
2045
+ */
2046
+ private checkTaskFunctions;
2047
+ private checkWorkerOptions;
2048
+ private endTaskPerformance;
2036
2049
  /**
2037
2050
  * Starts the worker check active interval.
2038
2051
  */
@@ -2041,51 +2054,38 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
2041
2054
  * Stops the worker check active interval.
2042
2055
  */
2043
2056
  private stopCheckActive;
2057
+ private updateLastTaskTimestamp;
2044
2058
  /**
2045
- * Checks if the worker should be terminated, because its living too long.
2046
- */
2047
- private checkActive;
2048
- /**
2049
- * Returns the main worker.
2050
- * @returns Reference to the main worker.
2051
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
2052
- */
2053
- protected getMainWorker(): MainWorker;
2054
- /**
2055
- * Sends a message to main worker.
2056
- * @param message - The response message.
2057
- */
2058
- protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
2059
- /**
2060
- * Sends task functions properties to the main worker.
2059
+ * Adds a task function to the worker.
2060
+ * If a task function with the same name already exists, it is replaced.
2061
+ * @param name - The name of the task function to add.
2062
+ * @param fn - The task function to add.
2063
+ * @returns Whether the task function was added or not.
2061
2064
  */
2062
- protected sendTaskFunctionsPropertiesToMainWorker(): void;
2065
+ addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): TaskFunctionOperationResult;
2063
2066
  /**
2064
- * Handles an error and convert it to a string so it can be sent back to the main worker.
2065
- * @param error - The error raised by the worker.
2066
- * @returns The error message.
2067
+ * Checks if the worker has a task function with the given name.
2068
+ * @param name - The name of the task function to check.
2069
+ * @returns Whether the worker has a task function with the given name or not.
2067
2070
  */
2068
- protected handleError(error: Error | string): string;
2071
+ hasTaskFunction(name: string): TaskFunctionOperationResult;
2069
2072
  /**
2070
- * Runs the given task.
2071
- * @param task - The task to execute.
2073
+ * Lists the properties of the worker's task functions.
2074
+ * @returns The properties of the worker's task functions.
2072
2075
  */
2073
- protected readonly run: (task: Task<Data>) => void;
2076
+ listTaskFunctionsProperties(): TaskFunctionProperties[];
2074
2077
  /**
2075
- * Runs the given task function synchronously.
2076
- * @param fn - Task function that will be executed.
2077
- * @param task - Input data for the task function.
2078
+ * Removes a task function from the worker.
2079
+ * @param name - The name of the task function to remove.
2080
+ * @returns Whether the task function existed and was removed or not.
2078
2081
  */
2079
- protected readonly runSync: (fn: TaskSyncFunction<Data, Response>, task: Task<Data>) => void;
2082
+ removeTaskFunction(name: string): TaskFunctionOperationResult;
2080
2083
  /**
2081
- * Runs the given task function asynchronously.
2082
- * @param fn - Task function that will be executed.
2083
- * @param task - Input data for the task function.
2084
+ * Sets the default task function to use in the worker.
2085
+ * @param name - The name of the task function to use as default task function.
2086
+ * @returns Whether the default task function was set or not.
2084
2087
  */
2085
- protected readonly runAsync: (fn: TaskAsyncFunction<Data, Response>, task: Task<Data>) => void;
2086
- private beginTaskPerformance;
2087
- private endTaskPerformance;
2088
- private updateLastTaskTimestamp;
2088
+ setDefaultTaskFunction(name: string): TaskFunctionOperationResult;
2089
2089
  }
2090
2090
 
2091
2091
  /**
@@ -2102,6 +2102,8 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
2102
2102
  * @since 2.0.0
2103
2103
  */
2104
2104
  declare class ClusterWorker<Data = unknown, Response = unknown> extends AbstractWorker<Worker, Data, Response> {
2105
+ /** @inheritDoc */
2106
+ protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2105
2107
  /**
2106
2108
  * Constructs a new poolifier cluster worker.
2107
2109
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
@@ -2112,8 +2114,6 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
2112
2114
  protected handleReadyMessage(message: MessageValue<Data>): void;
2113
2115
  /** @inheritDoc */
2114
2116
  protected get id(): number;
2115
- /** @inheritDoc */
2116
- protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2117
2117
  }
2118
2118
 
2119
2119
  /**
@@ -2130,6 +2130,8 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
2130
2130
  * @since 0.0.1
2131
2131
  */
2132
2132
  declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractWorker<MessagePort, Data, Response> {
2133
+ /** @inheritDoc */
2134
+ protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2133
2135
  /**
2134
2136
  * Message port used to communicate with the main worker.
2135
2137
  */
@@ -2140,18 +2142,16 @@ declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractW
2140
2142
  * @param opts - Options for the worker.
2141
2143
  */
2142
2144
  constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
2143
- /** @inheritDoc */
2144
- protected handleReadyMessage(message: MessageValue<Data>): void;
2145
- /** @inheritDoc */
2146
- protected handleKillMessage(message: MessageValue<Data>): void;
2147
- /** @inheritDoc */
2148
- protected get id(): number;
2149
- /** @inheritDoc */
2150
- protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2151
2145
  /**
2152
2146
  * @inheritDoc
2153
2147
  */
2154
2148
  protected handleError(error: Error | string): string;
2149
+ /** @inheritDoc */
2150
+ protected handleKillMessage(message: MessageValue<Data>): void;
2151
+ /** @inheritDoc */
2152
+ protected handleReadyMessage(message: MessageValue<Data>): void;
2153
+ /** @inheritDoc */
2154
+ protected get id(): number;
2155
2155
  }
2156
2156
 
2157
2157
  export { AbstractPool, AbstractWorker, CircularBuffer, type ClusterPoolOptions, ClusterWorker, DynamicClusterPool, DynamicThreadPool, type ErrorHandler, type EventHandler, type EventLoopUtilizationMeasurementStatistics, type ExitHandler, FixedClusterPool, type FixedQueueNode, FixedThreadPool, type IFixedQueue, type IPool, type IWorker, type IWorkerChoiceStrategy, type IWorkerNode, type KillBehavior, KillBehaviors, type KillHandler, type Measurement, type MeasurementOptions, type MeasurementStatistics, type MeasurementStatisticsRequirements, Measurements, type MessageHandler, type MessageValue, type OnlineHandler, type PoolEvent, PoolEvents, type PoolInfo, type PoolOptions, type PoolType, PoolTypes, PriorityQueue, type PromiseResponseWrapper, type StrategyData, type StrategyPolicy, type Task, type TaskAsyncFunction, type TaskFunction, type TaskFunctionObject, type TaskFunctionOperationResult, type TaskFunctionProperties, type TaskFunctions, type TaskPerformance, type TaskStatistics, type TaskStatisticsRequirements, type TaskSyncFunction, type TasksQueueOptions, type ThreadPoolOptions, ThreadWorker, WorkerChoiceStrategies, WorkerChoiceStrategiesContext, type WorkerChoiceStrategy, type WorkerChoiceStrategyOptions, type WorkerError, type WorkerInfo, type WorkerNodeEventDetail, type WorkerNodeOptions, type WorkerOptions, type WorkerStatistics, type WorkerType, WorkerTypes, type WorkerUsage, type Writable, availableParallelism };