poolifier 4.2.3 → 4.2.5

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,25 @@ 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.
821
- */
822
- readonly dequeueTask: (bucket?: number) => Task<Data> | undefined;
823
- /**
824
- * Dequeue last prioritized task.
825
- * @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.
826
806
  */
827
- readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined;
807
+ readonly getTaskFunctionWorkerUsage: (name: string) => undefined | WorkerUsage;
828
808
  /**
829
- * Clears tasks queue.
809
+ * Worker info.
830
810
  */
831
- readonly clearTasksQueue: () => void;
811
+ readonly info: WorkerInfo;
832
812
  /**
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.
813
+ * Message channel (worker thread only).
835
814
  */
836
- readonly hasBackPressure: () => boolean;
815
+ readonly messageChannel?: MessageChannel;
837
816
  /**
838
- * Terminates the worker node.
817
+ * Registers once a worker event handler.
818
+ * @param event - The event.
819
+ * @param handler - The event handler.
839
820
  */
840
- readonly terminate: () => Promise<void>;
821
+ readonly registerOnceWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
841
822
  /**
842
823
  * Registers a worker event handler.
843
824
  * @param event - The event.
@@ -845,23 +826,37 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
845
826
  */
846
827
  readonly registerWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
847
828
  /**
848
- * Registers once a worker event handler.
849
- * @param event - The event.
850
- * @param handler - The event handler.
829
+ * Sets tasks queue priority.
830
+ * @param enablePriority - Whether to enable tasks queue priority.
851
831
  */
852
- readonly registerOnceWorkerEventHandler: (event: string, handler: EventHandler<Worker>) => void;
832
+ readonly setTasksQueuePriority: (enablePriority: boolean) => void;
853
833
  /**
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.
834
+ * Worker choice strategy data.
835
+ * This is used to store data that are specific to the worker choice strategy.
857
836
  */
858
- readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined;
837
+ strategyData?: StrategyData;
859
838
  /**
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.
839
+ * Tasks queue back pressure size.
840
+ * This is the number of tasks that can be enqueued before the worker node has back pressure.
863
841
  */
864
- readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean;
842
+ tasksQueueBackPressureSize: number;
843
+ /**
844
+ * Tasks queue size.
845
+ * @returns The tasks queue size.
846
+ */
847
+ readonly tasksQueueSize: () => number;
848
+ /**
849
+ * Terminates the worker node.
850
+ */
851
+ readonly terminate: () => Promise<void>;
852
+ /**
853
+ * Worker usage statistics.
854
+ */
855
+ readonly usage: WorkerUsage;
856
+ /**
857
+ * Worker.
858
+ */
859
+ readonly worker: Worker;
865
860
  }
866
861
  /**
867
862
  * Worker node event detail.
@@ -876,8 +871,8 @@ interface WorkerNodeEventDetail {
876
871
  * Enumeration of pool types.
877
872
  */
878
873
  declare const PoolTypes: Readonly<{
879
- fixed: 'fixed';
880
874
  dynamic: 'dynamic';
875
+ fixed: 'fixed';
881
876
  }>;
882
877
  /**
883
878
  * Pool type.
@@ -887,14 +882,14 @@ type PoolType = keyof typeof PoolTypes;
887
882
  * Enumeration of pool events.
888
883
  */
889
884
  declare const PoolEvents: Readonly<{
890
- ready: 'ready';
885
+ backPressure: 'backPressure';
891
886
  busy: 'busy';
892
- full: 'full';
893
- empty: 'empty';
894
887
  destroy: 'destroy';
888
+ empty: 'empty';
895
889
  error: 'error';
890
+ full: 'full';
891
+ ready: 'ready';
896
892
  taskError: 'taskError';
897
- backPressure: 'backPressure';
898
893
  }>;
899
894
  /**
900
895
  * Pool event.
@@ -904,86 +899,86 @@ type PoolEvent = keyof typeof PoolEvents;
904
899
  * Pool information.
905
900
  */
906
901
  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;
902
+ readonly backPressure?: boolean;
926
903
  /** Pool tasks back pressure worker nodes. */
927
904
  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
- };
905
+ /** Pool busy worker nodes. */
906
+ readonly busyWorkerNodes: number;
907
+ readonly defaultStrategy: WorkerChoiceStrategy;
947
908
  readonly elu?: {
948
- idle: {
949
- readonly minimum: number;
950
- readonly maximum: number;
909
+ active: {
951
910
  readonly average?: number;
911
+ readonly maximum: number;
952
912
  readonly median?: number;
953
- };
954
- active: {
955
913
  readonly minimum: number;
956
- readonly maximum: number;
914
+ };
915
+ idle: {
957
916
  readonly average?: number;
917
+ readonly maximum: number;
958
918
  readonly median?: number;
919
+ readonly minimum: number;
959
920
  };
960
921
  utilization: {
961
922
  readonly average?: number;
962
923
  readonly median?: number;
963
924
  };
964
925
  };
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
983
- */
984
- readonly taskStealing?: boolean;
985
- /**
986
- * Whether to enable tasks stealing under back pressure.
926
+ readonly executedTasks: number;
927
+ readonly executingTasks: number;
928
+ readonly failedTasks: number;
929
+ /** Pool idle worker nodes. */
930
+ readonly idleWorkerNodes: number;
931
+ readonly maxQueuedTasks?: number;
932
+ readonly maxSize: number;
933
+ readonly minSize: number;
934
+ readonly queuedTasks?: number;
935
+ readonly ready: boolean;
936
+ readonly runTime?: {
937
+ readonly average?: number;
938
+ readonly maximum: number;
939
+ readonly median?: number;
940
+ readonly minimum: number;
941
+ };
942
+ readonly started: boolean;
943
+ /** Pool tasks stealing worker nodes. */
944
+ readonly stealingWorkerNodes?: number;
945
+ readonly stolenTasks?: number;
946
+ readonly strategyRetries: number;
947
+ readonly type: PoolType;
948
+ /** Pool utilization. */
949
+ readonly utilization?: number;
950
+ readonly version: string;
951
+ readonly waitTime?: {
952
+ readonly average?: number;
953
+ readonly maximum: number;
954
+ readonly median?: number;
955
+ readonly minimum: number;
956
+ };
957
+ readonly worker: WorkerType;
958
+ /** Pool total worker nodes. */
959
+ readonly workerNodes: number;
960
+ }
961
+ /**
962
+ * Worker node tasks queue options.
963
+ */
964
+ interface TasksQueueOptions {
965
+ /**
966
+ * Maximum number of tasks that can be executed concurrently on a worker node.
967
+ * @defaultValue 1
968
+ */
969
+ readonly concurrency?: number;
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
+ * Queued tasks finished timeout in milliseconds at worker node termination.
977
+ * @defaultValue 2000
978
+ */
979
+ readonly tasksFinishedTimeout?: number;
980
+ /**
981
+ * Whether to enable tasks stealing under back pressure.
987
982
  * @defaultValue true
988
983
  */
989
984
  readonly tasksStealingOnBackPressure?: boolean;
@@ -993,10 +988,10 @@ interface TasksQueueOptions {
993
988
  */
994
989
  readonly tasksStealingRatio?: number;
995
990
  /**
996
- * Queued tasks finished timeout in milliseconds at worker node termination.
997
- * @defaultValue 2000
991
+ * Whether to enable task stealing on idle.
992
+ * @defaultValue true
998
993
  */
999
- readonly tasksFinishedTimeout?: number;
994
+ readonly taskStealing?: boolean;
1000
995
  }
1001
996
  /**
1002
997
  * Options for a poolifier pool.
@@ -1004,15 +999,20 @@ interface TasksQueueOptions {
1004
999
  */
1005
1000
  interface PoolOptions<Worker extends IWorker> {
1006
1001
  /**
1007
- * A function that will listen for online event on each worker.
1008
- * @defaultValue `() => {}`
1002
+ * Pool events integrated with async resource emission.
1003
+ * @defaultValue true
1009
1004
  */
1010
- onlineHandler?: OnlineHandler<Worker>;
1005
+ enableEvents?: boolean;
1011
1006
  /**
1012
- * A function that will listen for message event on each worker.
1013
- * @defaultValue `() => {}`
1007
+ * Pool worker node tasks queue.
1008
+ * @defaultValue false
1014
1009
  */
1015
- messageHandler?: MessageHandler<Worker>;
1010
+ enableTasksQueue?: boolean;
1011
+ /**
1012
+ * Key/value pairs to add to worker process environment.
1013
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1014
+ */
1015
+ env?: Record<string, unknown>;
1016
1016
  /**
1017
1017
  * A function that will listen for error event on each worker.
1018
1018
  * @defaultValue `() => {}`
@@ -1024,52 +1024,47 @@ interface PoolOptions<Worker extends IWorker> {
1024
1024
  */
1025
1025
  exitHandler?: ExitHandler<Worker>;
1026
1026
  /**
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
1027
+ * A function that will listen for message event on each worker.
1028
+ * @defaultValue `() => {}`
1034
1029
  */
1035
- workerChoiceStrategy?: WorkerChoiceStrategy;
1030
+ messageHandler?: MessageHandler<Worker>;
1036
1031
  /**
1037
- * The worker choice strategy options.
1032
+ * A function that will listen for online event on each worker.
1033
+ * @defaultValue `() => {}`
1038
1034
  */
1039
- workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
1035
+ onlineHandler?: OnlineHandler<Worker>;
1040
1036
  /**
1041
1037
  * Restart worker on error.
1042
1038
  */
1043
1039
  restartWorkerOnError?: boolean;
1044
1040
  /**
1045
- * Pool events integrated with async resource emission.
1046
- * @defaultValue true
1041
+ * Cluster settings.
1042
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1047
1043
  */
1048
- enableEvents?: boolean;
1044
+ settings?: ClusterSettings;
1049
1045
  /**
1050
- * Pool worker node tasks queue.
1051
- * @defaultValue false
1046
+ * Whether to start the minimum number of workers at pool initialization.
1047
+ * @defaultValue true
1052
1048
  */
1053
- enableTasksQueue?: boolean;
1049
+ startWorkers?: boolean;
1054
1050
  /**
1055
1051
  * Pool worker node tasks queue options.
1056
1052
  */
1057
1053
  tasksQueueOptions?: TasksQueueOptions;
1058
1054
  /**
1059
- * Worker options.
1060
- * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1055
+ * The default worker choice strategy to use in this pool.
1056
+ * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
1061
1057
  */
1062
- workerOptions?: WorkerOptions$1;
1058
+ workerChoiceStrategy?: WorkerChoiceStrategy;
1063
1059
  /**
1064
- * Key/value pairs to add to worker process environment.
1065
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1060
+ * The worker choice strategy options.
1066
1061
  */
1067
- env?: Record<string, unknown>;
1062
+ workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
1068
1063
  /**
1069
- * Cluster settings.
1070
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1064
+ * Worker options.
1065
+ * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1071
1066
  */
1072
- settings?: ClusterSettings;
1067
+ workerOptions?: WorkerOptions$1;
1073
1068
  }
1074
1069
  /**
1075
1070
  * Contract definition for a poolifier pool.
@@ -1079,14 +1074,19 @@ interface PoolOptions<Worker extends IWorker> {
1079
1074
  */
1080
1075
  interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1081
1076
  /**
1082
- * Pool information.
1077
+ * Adds a task function to this pool.
1078
+ * If a task function with the same name already exists, it will be overwritten.
1079
+ * @param name - The name of the task function.
1080
+ * @param fn - The task function.
1081
+ * @returns `true` if the task function was added, `false` otherwise.
1082
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
1083
+ * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function or task function object.
1083
1084
  */
1084
- readonly info: PoolInfo;
1085
+ readonly addTaskFunction: (name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>) => Promise<boolean>;
1085
1086
  /**
1086
- * Pool worker nodes.
1087
- * @internal
1087
+ * Terminates all workers in this pool.
1088
1088
  */
1089
- readonly workerNodes: IWorkerNode<Worker, Data>[];
1089
+ readonly destroy: () => Promise<void>;
1090
1090
  /**
1091
1091
  * Pool event emitter integrated with async resource.
1092
1092
  * The async tracking tooling identifier is `poolifier:<PoolType>-<WorkerType>-pool`.
@@ -1103,6 +1103,12 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1103
1103
  * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
1104
1104
  */
1105
1105
  readonly emitter?: EventEmitterAsyncResource;
1106
+ /**
1107
+ * Enables/disables the worker node tasks queue in this pool.
1108
+ * @param enable - Whether to enable or disable the worker node tasks queue.
1109
+ * @param tasksQueueOptions - The worker node tasks queue options.
1110
+ */
1111
+ readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
1106
1112
  /**
1107
1113
  * Executes the specified function in the worker constructor with the task data input parameter.
1108
1114
  * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
@@ -1111,22 +1117,6 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1111
1117
  * @returns Promise with a task function response that will be fulfilled when the task is completed.
1112
1118
  */
1113
1119
  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
1120
  /**
1131
1121
  * Whether the specified task function exists in this pool.
1132
1122
  * @param name - The name of the task function.
@@ -1134,32 +1124,39 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1134
1124
  */
1135
1125
  readonly hasTaskFunction: (name: string) => boolean;
1136
1126
  /**
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.
1127
+ * Pool information.
1144
1128
  */
1145
- readonly addTaskFunction: (name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>) => Promise<boolean>;
1129
+ readonly info: PoolInfo;
1130
+ /**
1131
+ * Lists the properties of task functions available in this pool.
1132
+ * @returns The properties of task functions available in this pool.
1133
+ */
1134
+ readonly listTaskFunctionsProperties: () => TaskFunctionProperties[];
1135
+ /**
1136
+ * Executes the specified function in the worker constructor with the tasks data iterable input parameter.
1137
+ * @param data - The tasks iterable input data for the specified task function. This can only be an iterable of structured-cloneable data.
1138
+ * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
1139
+ * @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.
1140
+ * @returns Promise with an array of task function responses that will be fulfilled when the tasks are completed.
1141
+ */
1142
+ readonly mapExecute: (data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]) => Promise<Response[]>;
1146
1143
  /**
1147
1144
  * Removes a task function from this pool.
1148
1145
  * @param name - The name of the task function.
1149
1146
  * @returns `true` if the task function was removed, `false` otherwise.
1150
1147
  */
1151
1148
  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
1149
  /**
1158
1150
  * Sets the default task function in this pool.
1159
1151
  * @param name - The name of the task function.
1160
1152
  * @returns `true` if the default task function was set, `false` otherwise.
1161
1153
  */
1162
1154
  readonly setDefaultTaskFunction: (name: string) => Promise<boolean>;
1155
+ /**
1156
+ * Sets the worker node tasks queue options in this pool.
1157
+ * @param tasksQueueOptions - The worker node tasks queue options.
1158
+ */
1159
+ readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
1163
1160
  /**
1164
1161
  * Sets the default worker choice strategy in this pool.
1165
1162
  * @param workerChoiceStrategy - The default worker choice strategy.
@@ -1173,16 +1170,14 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1173
1170
  */
1174
1171
  readonly setWorkerChoiceStrategyOptions: (workerChoiceStrategyOptions: WorkerChoiceStrategyOptions) => boolean;
1175
1172
  /**
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.
1173
+ * Starts the minimum number of workers in this pool.
1179
1174
  */
1180
- readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
1175
+ readonly start: () => void;
1181
1176
  /**
1182
- * Sets the worker node tasks queue options in this pool.
1183
- * @param tasksQueueOptions - The worker node tasks queue options.
1177
+ * Pool worker nodes.
1178
+ * @internal
1184
1179
  */
1185
- readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
1180
+ readonly workerNodes: IWorkerNode<Worker, Data>[];
1186
1181
  }
1187
1182
 
1188
1183
  /**
@@ -1194,14 +1189,14 @@ interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
1194
1189
  */
1195
1190
  declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unknown, Response = unknown> {
1196
1191
  private readonly pool;
1197
- /**
1198
- * The number of worker choice strategies execution retries.
1199
- */
1200
- retriesCount: number;
1201
1192
  /**
1202
1193
  * The default worker choice strategy in the context.
1203
1194
  */
1204
1195
  private defaultWorkerChoiceStrategy;
1196
+ /**
1197
+ * The maximum number of worker choice strategies execution retries.
1198
+ */
1199
+ private readonly retries;
1205
1200
  /**
1206
1201
  * The worker choice strategies registered in the context.
1207
1202
  */
@@ -1215,9 +1210,9 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1215
1210
  */
1216
1211
  private workerChoiceStrategiesTaskStatisticsRequirements;
1217
1212
  /**
1218
- * The maximum number of worker choice strategies execution retries.
1213
+ * The number of worker choice strategies execution retries.
1219
1214
  */
1220
- private readonly retries;
1215
+ retriesCount: number;
1221
1216
  /**
1222
1217
  * Worker choice strategies context constructor.
1223
1218
  * @param pool - The pool instance.
@@ -1225,6 +1220,34 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1225
1220
  * @param opts - The worker choice strategy options.
1226
1221
  */
1227
1222
  constructor(pool: IPool<Worker, Data, Response>, workerChoiceStrategies?: WorkerChoiceStrategy[], opts?: WorkerChoiceStrategyOptions);
1223
+ /**
1224
+ * Adds a worker choice strategy to the context.
1225
+ * @param workerChoiceStrategy - The worker choice strategy to add.
1226
+ * @param pool - The pool instance.
1227
+ * @param opts - The worker choice strategy options.
1228
+ * @returns The worker choice strategies.
1229
+ */
1230
+ private addWorkerChoiceStrategy;
1231
+ /**
1232
+ * Executes the given worker choice strategy.
1233
+ * @param workerChoiceStrategy - The worker choice strategy.
1234
+ * @returns The key of the worker node.
1235
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1236
+ */
1237
+ private executeStrategy;
1238
+ /**
1239
+ * Removes a worker choice strategy from the context.
1240
+ * @param workerChoiceStrategy - The worker choice strategy to remove.
1241
+ * @returns `true` if the worker choice strategy is removed, `false` otherwise.
1242
+ */
1243
+ private removeWorkerChoiceStrategy;
1244
+ /**
1245
+ * Executes the given worker choice strategy in the context algorithm.
1246
+ * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
1247
+ * @returns The key of the worker node.
1248
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
1249
+ */
1250
+ execute(workerChoiceStrategy?: WorkerChoiceStrategy): number;
1228
1251
  /**
1229
1252
  * Gets the active worker choice strategies in the context policy.
1230
1253
  * @returns The strategies policy.
@@ -1235,43 +1258,23 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1235
1258
  * @returns The strategies task statistics requirements.
1236
1259
  */
1237
1260
  getTaskStatisticsRequirements(): TaskStatisticsRequirements;
1261
+ /**
1262
+ * Removes the worker node key from the active worker choice strategies in the context.
1263
+ * @param workerNodeKey - The worker node key.
1264
+ * @returns `true` if the removal is successful, `false` otherwise.
1265
+ */
1266
+ remove(workerNodeKey: number): boolean;
1238
1267
  /**
1239
1268
  * Sets the default worker choice strategy to use in the context.
1240
1269
  * @param workerChoiceStrategy - The default worker choice strategy to set.
1241
1270
  * @param opts - The worker choice strategy options.
1242
1271
  */
1243
1272
  setDefaultWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, opts?: WorkerChoiceStrategyOptions): void;
1244
- /**
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.
1248
- */
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
1273
  /**
1271
1274
  * Sets the active worker choice strategies in the context options.
1272
1275
  * @param opts - The worker choice strategy options.
1273
1276
  */
1274
- setOptions(opts: WorkerChoiceStrategyOptions | undefined): void;
1277
+ setOptions(opts: undefined | WorkerChoiceStrategyOptions): void;
1275
1278
  /**
1276
1279
  * Synchronizes the active worker choice strategies in the context with the given worker choice strategies.
1277
1280
  * @param workerChoiceStrategies - The worker choice strategies to synchronize.
@@ -1279,19 +1282,11 @@ declare class WorkerChoiceStrategiesContext<Worker extends IWorker, Data = unkno
1279
1282
  */
1280
1283
  syncWorkerChoiceStrategies(workerChoiceStrategies: Set<WorkerChoiceStrategy>, opts?: WorkerChoiceStrategyOptions): void;
1281
1284
  /**
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.
1285
+ * Updates the worker node key in the active worker choice strategies in the context internals.
1286
+ * @param workerNodeKey - The worker node key.
1287
+ * @returns `true` if the update is successful, `false` otherwise.
1293
1288
  */
1294
- private removeWorkerChoiceStrategy;
1289
+ update(workerNodeKey: number): boolean;
1295
1290
  }
1296
1291
 
1297
1292
  /**
@@ -1305,10 +1300,6 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1305
1300
  protected readonly filePath: string;
1306
1301
  protected readonly opts: PoolOptions<Worker>;
1307
1302
  protected readonly maximumNumberOfWorkers?: number | undefined;
1308
- /** @inheritDoc */
1309
- readonly workerNodes: IWorkerNode<Worker, Data>[];
1310
- /** @inheritDoc */
1311
- emitter?: EventEmitterAsyncResource;
1312
1303
  /**
1313
1304
  * The task execution response promise map:
1314
1305
  * - `key`: The message id of each submitted task.
@@ -1322,35 +1313,73 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1322
1313
  */
1323
1314
  protected workerChoiceStrategiesContext?: WorkerChoiceStrategiesContext<Worker, Data, Response>;
1324
1315
  /**
1325
- * The task functions added at runtime map:
1326
- * - `key`: The task function name.
1327
- * - `value`: The task function object.
1316
+ * This method is the message listener registered on each worker.
1317
+ * @param message - The message received from the worker.
1328
1318
  */
1329
- private readonly taskFunctions;
1319
+ protected readonly workerMessageListener: (message: MessageValue<Response>) => void;
1330
1320
  /**
1331
- * Whether the pool is started or not.
1321
+ * Whether the pool is destroying or not.
1332
1322
  */
1333
- private started;
1323
+ private destroying;
1334
1324
  /**
1335
- * Whether the pool is starting or not.
1325
+ * Gets task function worker choice strategy, if any.
1326
+ * @param name - The task function name.
1327
+ * @returns The task function worker choice strategy if the task function worker choice strategy is defined, `undefined` otherwise.
1336
1328
  */
1337
- private starting;
1329
+ private readonly getTaskFunctionWorkerChoiceStrategy;
1338
1330
  /**
1339
- * Whether the pool is destroying or not.
1331
+ * Gets the worker choice strategies registered in this pool.
1332
+ * @returns The worker choice strategies.
1340
1333
  */
1341
- private destroying;
1334
+ private readonly getWorkerChoiceStrategies;
1342
1335
  /**
1343
- * Whether the minimum number of workers is starting or not.
1336
+ * Gets worker node task function priority, if any.
1337
+ * @param workerNodeKey - The worker node key.
1338
+ * @param name - The task function name.
1339
+ * @returns The worker node task function priority if the worker node task function priority is defined, `undefined` otherwise.
1344
1340
  */
1345
- private startingMinimumNumberOfWorkers;
1341
+ private readonly getWorkerNodeTaskFunctionPriority;
1342
+ /**
1343
+ * Gets worker node task function worker choice strategy, if any.
1344
+ * @param workerNodeKey - The worker node key.
1345
+ * @param name - The task function name.
1346
+ * @returns The worker node task function worker choice strategy if the worker node task function worker choice strategy is defined, `undefined` otherwise.
1347
+ */
1348
+ private readonly getWorkerNodeTaskFunctionWorkerChoiceStrategy;
1349
+ private readonly handleWorkerNodeBackPressureEvent;
1350
+ private readonly handleWorkerNodeIdleEvent;
1346
1351
  /**
1347
1352
  * Whether the pool ready event has been emitted or not.
1348
1353
  */
1349
1354
  private readyEventEmitted;
1355
+ /**
1356
+ * Whether the pool is started or not.
1357
+ */
1358
+ private started;
1359
+ /**
1360
+ * Whether the pool is starting or not.
1361
+ */
1362
+ private starting;
1363
+ /**
1364
+ * Whether the minimum number of workers is starting or not.
1365
+ */
1366
+ private startingMinimumNumberOfWorkers;
1350
1367
  /**
1351
1368
  * The start timestamp of the pool.
1352
1369
  */
1353
1370
  private startTimestamp?;
1371
+ private readonly stealTask;
1372
+ /**
1373
+ * The task functions added at runtime map:
1374
+ * - `key`: The task function name.
1375
+ * - `value`: The task function object.
1376
+ */
1377
+ private readonly taskFunctions;
1378
+ private readonly workerNodeStealTask;
1379
+ /** @inheritDoc */
1380
+ emitter?: EventEmitterAsyncResource;
1381
+ /** @inheritDoc */
1382
+ readonly workerNodes: IWorkerNode<Worker, Data>[];
1354
1383
  /**
1355
1384
  * Constructs a new poolifier pool.
1356
1385
  * @param minimumNumberOfWorkers - Minimum number of workers that this pool manages.
@@ -1359,169 +1388,126 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1359
1388
  * @param maximumNumberOfWorkers - Maximum number of workers that this pool manages.
1360
1389
  */
1361
1390
  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
1391
  /**
1370
- * Whether the pool is ready or not.
1371
- * @returns The pool readiness boolean status.
1392
+ * Hook executed after the worker task execution.
1393
+ * Can be overridden.
1394
+ * @param workerNodeKey - The worker node key.
1395
+ * @param message - The received message.
1372
1396
  */
1373
- private get ready();
1397
+ protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
1374
1398
  /**
1375
- * Whether the pool is empty or not.
1376
- * @returns The pool emptiness boolean status.
1399
+ * Method hooked up after a worker node has been newly created.
1400
+ * Can be overridden.
1401
+ * @param workerNodeKey - The newly created worker node key.
1377
1402
  */
1378
- protected get empty(): boolean;
1403
+ protected afterWorkerNodeSetup(workerNodeKey: number): void;
1379
1404
  /**
1380
- * The approximate pool utilization.
1381
- * @returns The pool utilization.
1405
+ * Hook executed before the worker task execution.
1406
+ * Can be overridden.
1407
+ * @param workerNodeKey - The worker node key.
1408
+ * @param task - The task to execute.
1382
1409
  */
1383
- private get utilization();
1410
+ protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
1384
1411
  /**
1385
- * The pool type.
1386
- *
1387
- * If it is `'dynamic'`, it provides the `max` property.
1412
+ * Emits dynamic worker creation events.
1388
1413
  */
1389
- protected abstract get type(): PoolType;
1414
+ protected abstract checkAndEmitDynamicWorkerCreationEvents(): void;
1390
1415
  /**
1391
- * The worker type.
1416
+ * Creates a new, completely set up dynamic worker node.
1417
+ * @returns New, completely set up dynamic worker node key.
1392
1418
  */
1393
- protected abstract get worker(): WorkerType;
1419
+ protected createAndSetupDynamicWorkerNode(): number;
1394
1420
  /**
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.
1421
+ * Creates a new, completely set up worker node.
1422
+ * @returns New, completely set up worker node key.
1398
1423
  */
1399
- private checkMessageWorkerId;
1424
+ protected createAndSetupWorkerNode(): number;
1400
1425
  /**
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.
1426
+ * Deregisters a listener callback on the worker given its worker node key.
1427
+ * @param workerNodeKey - The worker node key.
1428
+ * @param listener - The message listener callback.
1404
1429
  */
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;
1430
+ protected abstract deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1420
1431
  /**
1421
- * Whether the pool is full or not.
1422
- * @returns The pool fullness boolean status.
1432
+ * Terminates the worker node given its worker node key.
1433
+ * @param workerNodeKey - The worker node key.
1423
1434
  */
1424
- protected get full(): boolean;
1435
+ protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1436
+ protected flagWorkerNodeAsNotReady(workerNodeKey: number): void;
1437
+ protected flushTasksQueue(workerNodeKey: number): number;
1425
1438
  /**
1426
- * Whether the pool is busy or not.
1427
- * @returns The pool busyness boolean status.
1439
+ * Gets the worker information given its worker node key.
1440
+ * @param workerNodeKey - The worker node key.
1441
+ * @returns The worker information.
1428
1442
  */
1429
- protected abstract get busy(): boolean;
1443
+ protected getWorkerInfo(workerNodeKey: number): undefined | WorkerInfo;
1430
1444
  /**
1431
1445
  * Whether worker nodes are executing concurrently their tasks quota or not.
1432
1446
  * @returns Worker nodes busyness boolean status.
1433
1447
  */
1434
1448
  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
1449
  /**
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.
1450
+ * Returns whether the worker is the main worker or not.
1451
+ * @returns `true` if the worker is the main worker, `false` otherwise.
1451
1452
  */
1452
- private readonly getTaskFunctionWorkerChoiceStrategy;
1453
+ protected abstract isMain(): boolean;
1453
1454
  /**
1454
- * Gets worker node task function worker choice strategy, if any.
1455
+ * Registers once a listener callback on the worker given its worker node key.
1455
1456
  * @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.
1457
+ * @param listener - The message listener callback.
1458
1458
  */
1459
- private readonly getWorkerNodeTaskFunctionWorkerChoiceStrategy;
1459
+ protected abstract registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1460
1460
  /**
1461
- * Gets worker node task function priority, if any.
1461
+ * Registers a listener callback on the worker given its worker node key.
1462
1462
  * @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.
1463
+ * @param listener - The message listener callback.
1470
1464
  */
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[]>;
1465
+ protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1480
1466
  /**
1481
- * Starts the minimum number of workers.
1482
- * @param initWorkerNodeUsage - Whether to initialize the worker node usage or not. @defaultValue false
1467
+ * Sends the startup message to worker given its worker node key.
1468
+ * @param workerNodeKey - The worker node key.
1483
1469
  */
1484
- private startMinimumNumberOfWorkers;
1485
- /** @inheritdoc */
1486
- start(): void;
1487
- /** @inheritDoc */
1488
- destroy(): Promise<void>;
1489
- private sendKillMessageToWorker;
1470
+ protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
1490
1471
  /**
1491
- * Terminates the worker node given its worker node key.
1472
+ * Sends a message to worker given its worker node key.
1492
1473
  * @param workerNodeKey - The worker node key.
1474
+ * @param message - The message.
1475
+ * @param transferList - The optional array of transferable objects.
1493
1476
  */
1494
- protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1477
+ protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1495
1478
  /**
1496
1479
  * Setup hook to execute code before worker nodes are created in the abstract constructor.
1497
1480
  * Can be overridden.
1498
1481
  */
1499
1482
  protected setupHook(): void;
1500
1483
  /**
1501
- * Returns whether the worker is the main worker or not.
1502
- * @returns `true` if the worker is the main worker, `false` otherwise.
1484
+ * Conditions for dynamic worker creation.
1485
+ * @returns Whether to create a dynamic worker or not.
1503
1486
  */
1504
- protected abstract isMain(): boolean;
1487
+ protected abstract shallCreateDynamicWorker(): boolean;
1505
1488
  /**
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.
1489
+ * Adds the given worker node in the pool worker nodes.
1490
+ * @param workerNode - The worker node.
1491
+ * @returns The added worker node key.
1492
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1510
1493
  */
1511
- protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
1494
+ private addWorkerNode;
1495
+ private buildTasksQueueOptions;
1496
+ private cannotStealTask;
1497
+ private checkAndEmitEmptyEvent;
1498
+ private checkAndEmitReadyEvent;
1499
+ private checkAndEmitTaskExecutionEvents;
1500
+ private checkAndEmitTaskQueuingEvents;
1512
1501
  /**
1513
- * Hook executed after the worker task execution.
1514
- * Can be overridden.
1515
- * @param workerNodeKey - The worker node key.
1502
+ * Checks if the worker id sent in the received message from a worker is valid.
1516
1503
  * @param message - The received message.
1504
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
1517
1505
  */
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;
1506
+ private checkMessageWorkerId;
1507
+ private checkMinimumNumberOfWorkers;
1508
+ private checkPoolOptions;
1509
+ private checkPoolType;
1510
+ private checkValidWorkerChoiceStrategyOptions;
1525
1511
  /**
1526
1512
  * Chooses a worker node for the next task.
1527
1513
  * @param name - The task function name.
@@ -1529,129 +1515,138 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1529
1515
  */
1530
1516
  private chooseWorkerNode;
1531
1517
  /**
1532
- * Conditions for dynamic worker creation.
1533
- * @returns Whether to create a dynamic worker or not.
1518
+ * Creates a worker node.
1519
+ * @returns The created worker node.
1534
1520
  */
1535
- protected abstract shallCreateDynamicWorker(): boolean;
1521
+ private createWorkerNode;
1522
+ private dequeueTask;
1523
+ private enqueueTask;
1536
1524
  /**
1537
- * Sends a message to worker given its worker node key.
1525
+ * Executes the given task on the worker given its worker node key.
1538
1526
  * @param workerNodeKey - The worker node key.
1539
- * @param message - The message.
1540
- * @param transferList - The optional array of transferable objects.
1527
+ * @param task - The task to execute.
1541
1528
  */
1542
- protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1529
+ private executeTask;
1530
+ private flushTasksQueues;
1531
+ private getTasksQueuePriority;
1532
+ /**
1533
+ * Gets the worker node key given its worker id.
1534
+ * @param workerId - The worker id.
1535
+ * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
1536
+ */
1537
+ private getWorkerNodeKeyByWorkerId;
1538
+ private handleTask;
1539
+ private handleTaskExecutionResponse;
1540
+ private handleWorkerReadyResponse;
1541
+ private hasBackPressure;
1542
+ private initEventEmitter;
1543
1543
  /**
1544
1544
  * Initializes the worker node usage with sensible default values gathered during runtime.
1545
1545
  * @param workerNode - The worker node.
1546
1546
  */
1547
1547
  private initWorkerNodeUsage;
1548
+ private internalExecute;
1549
+ private isWorkerNodeBusy;
1550
+ private isWorkerNodeIdle;
1551
+ private redistributeQueuedTasks;
1548
1552
  /**
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.
1553
+ * Removes the worker node from the pool worker nodes.
1554
+ * @param workerNode - The worker node.
1568
1555
  */
1569
- protected abstract registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1556
+ private removeWorkerNode;
1557
+ private resetTaskSequentiallyStolenStatisticsWorkerUsage;
1558
+ private sendKillMessageToWorker;
1570
1559
  /**
1571
- * Deregisters a listener callback on the worker given its worker node key.
1560
+ * Sends the statistics message to worker given its worker node key.
1572
1561
  * @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
1562
  */
1581
- protected afterWorkerNodeSetup(workerNodeKey: number): void;
1563
+ private sendStatisticsMessageToWorker;
1564
+ private sendTaskFunctionOperationToWorker;
1565
+ private sendTaskFunctionOperationToWorkers;
1566
+ private setTasksQueuePriority;
1567
+ private setTasksQueueSize;
1568
+ private setTasksStealingOnBackPressure;
1569
+ private setTaskStealing;
1570
+ private shallExecuteTask;
1582
1571
  /**
1583
- * Sends the startup message to worker given its worker node key.
1572
+ * Whether the worker node shall update its task function worker usage or not.
1584
1573
  * @param workerNodeKey - The worker node key.
1574
+ * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
1585
1575
  */
1586
- protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
1576
+ private shallUpdateTaskFunctionWorkerUsage;
1587
1577
  /**
1588
- * Sends the statistics message to worker given its worker node key.
1589
- * @param workerNodeKey - The worker node key.
1578
+ * Starts the minimum number of workers.
1579
+ * @param initWorkerNodeUsage - Whether to initialize the worker node usage or not. @defaultValue false
1590
1580
  */
1591
- private sendStatisticsMessageToWorker;
1592
- private cannotStealTask;
1593
- private handleTask;
1594
- private redistributeQueuedTasks;
1595
- private updateTaskStolenStatisticsWorkerUsage;
1581
+ private startMinimumNumberOfWorkers;
1582
+ private tasksQueueSize;
1583
+ private unsetTasksStealingOnBackPressure;
1584
+ private unsetTaskStealing;
1596
1585
  private updateTaskSequentiallyStolenStatisticsWorkerUsage;
1597
- private resetTaskSequentiallyStolenStatisticsWorkerUsage;
1598
- private readonly stealTask;
1599
- private readonly handleWorkerNodeIdleEvent;
1600
- private readonly workerNodeStealTask;
1601
- private readonly handleWorkerNodeBackPressureEvent;
1602
- private setTasksQueuePriority;
1586
+ private updateTaskStolenStatisticsWorkerUsage;
1587
+ /** @inheritDoc */
1588
+ addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): Promise<boolean>;
1589
+ /** @inheritDoc */
1590
+ destroy(): Promise<void>;
1591
+ /** @inheritDoc */
1592
+ enableTasksQueue(enable: boolean, tasksQueueOptions?: TasksQueueOptions): void;
1593
+ /** @inheritDoc */
1594
+ execute(data?: Data, name?: string, transferList?: readonly TransferListItem[]): Promise<Response>;
1595
+ /** @inheritDoc */
1596
+ hasTaskFunction(name: string): boolean;
1597
+ /** @inheritDoc */
1598
+ listTaskFunctionsProperties(): TaskFunctionProperties[];
1599
+ /** @inheritDoc */
1600
+ mapExecute(data: Iterable<Data>, name?: string, transferList?: readonly TransferListItem[]): Promise<Response[]>;
1601
+ /** @inheritDoc */
1602
+ removeTaskFunction(name: string): Promise<boolean>;
1603
+ /** @inheritDoc */
1604
+ setDefaultTaskFunction(name: string): Promise<boolean>;
1605
+ /** @inheritDoc */
1606
+ setTasksQueueOptions(tasksQueueOptions: TasksQueueOptions | undefined): void;
1607
+ /** @inheritDoc */
1608
+ setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions): void;
1609
+ /** @inheritDoc */
1610
+ setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions: undefined | WorkerChoiceStrategyOptions): boolean;
1611
+ /** @inheritdoc */
1612
+ start(): void;
1603
1613
  /**
1604
- * This method is the message listener registered on each worker.
1605
- * @param message - The message received from the worker.
1614
+ * Whether the pool is busy or not.
1615
+ * @returns The pool busyness boolean status.
1606
1616
  */
1607
- protected readonly workerMessageListener: (message: MessageValue<Response>) => void;
1608
- private checkAndEmitReadyEvent;
1609
- private handleWorkerReadyResponse;
1610
- private handleTaskExecutionResponse;
1611
- private checkAndEmitTaskExecutionEvents;
1612
- private checkAndEmitTaskQueuingEvents;
1617
+ protected abstract get busy(): boolean;
1613
1618
  /**
1614
- * Emits dynamic worker creation events.
1619
+ * Whether the pool is empty or not.
1620
+ * @returns The pool emptiness boolean status.
1615
1621
  */
1616
- protected abstract checkAndEmitDynamicWorkerCreationEvents(): void;
1622
+ protected get empty(): boolean;
1617
1623
  /**
1618
- * Gets the worker information given its worker node key.
1619
- * @param workerNodeKey - The worker node key.
1620
- * @returns The worker information.
1624
+ * Whether the pool is full or not.
1625
+ * @returns The pool fullness boolean status.
1621
1626
  */
1622
- protected getWorkerInfo(workerNodeKey: number): WorkerInfo | undefined;
1623
- private getTasksQueuePriority;
1627
+ protected get full(): boolean;
1628
+ /** @inheritDoc */
1629
+ get info(): PoolInfo;
1624
1630
  /**
1625
- * Creates a worker node.
1626
- * @returns The created worker node.
1631
+ * Whether the pool is ready or not.
1632
+ * @returns The pool readiness boolean status.
1627
1633
  */
1628
- private createWorkerNode;
1634
+ private get ready();
1629
1635
  /**
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.
1636
+ * The pool type.
1637
+ *
1638
+ * If it is `'dynamic'`, it provides the `max` property.
1634
1639
  */
1635
- private addWorkerNode;
1636
- private checkAndEmitEmptyEvent;
1640
+ protected abstract get type(): PoolType;
1637
1641
  /**
1638
- * Removes the worker node from the pool worker nodes.
1639
- * @param workerNode - The worker node.
1642
+ * The approximate pool utilization.
1643
+ * @returns The pool utilization.
1640
1644
  */
1641
- private removeWorkerNode;
1642
- protected flagWorkerNodeAsNotReady(workerNodeKey: number): void;
1643
- private hasBackPressure;
1645
+ private get utilization();
1644
1646
  /**
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.
1647
+ * The worker type.
1648
1648
  */
1649
- private executeTask;
1650
- private enqueueTask;
1651
- private dequeueTask;
1652
- private tasksQueueSize;
1653
- protected flushTasksQueue(workerNodeKey: number): number;
1654
- private flushTasksQueues;
1649
+ protected abstract get worker(): WorkerType;
1655
1650
  }
1656
1651
 
1657
1652
  /**
@@ -1675,29 +1670,29 @@ declare class FixedClusterPool<Data = unknown, Response = unknown> extends Abstr
1675
1670
  */
1676
1671
  constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions, maximumNumberOfWorkers?: number);
1677
1672
  /** @inheritDoc */
1678
- protected setupHook(): void;
1673
+ protected checkAndEmitDynamicWorkerCreationEvents(): void;
1679
1674
  /** @inheritDoc */
1680
- protected isMain(): boolean;
1675
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1681
1676
  /** @inheritDoc */
1682
- protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
1677
+ protected isMain(): boolean;
1683
1678
  /** @inheritDoc */
1684
- protected sendStartupMessageToWorker(workerNodeKey: number): void;
1679
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1685
1680
  /** @inheritDoc */
1686
1681
  protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1687
1682
  /** @inheritDoc */
1688
- protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1683
+ protected sendStartupMessageToWorker(workerNodeKey: number): void;
1689
1684
  /** @inheritDoc */
1690
- protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1685
+ protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
1686
+ /** @inheritDoc */
1687
+ protected setupHook(): void;
1691
1688
  /** @inheritDoc */
1692
1689
  protected shallCreateDynamicWorker(): boolean;
1693
1690
  /** @inheritDoc */
1694
- protected checkAndEmitDynamicWorkerCreationEvents(): void;
1691
+ protected get busy(): boolean;
1695
1692
  /** @inheritDoc */
1696
1693
  protected get type(): PoolType;
1697
1694
  /** @inheritDoc */
1698
1695
  protected get worker(): WorkerType;
1699
- /** @inheritDoc */
1700
- protected get busy(): boolean;
1701
1696
  }
1702
1697
 
1703
1698
  /**
@@ -1720,13 +1715,13 @@ declare class DynamicClusterPool<Data = unknown, Response = unknown> extends Fix
1720
1715
  */
1721
1716
  constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
1722
1717
  /** @inheritDoc */
1723
- protected shallCreateDynamicWorker(): boolean;
1724
- /** @inheritDoc */
1725
1718
  protected checkAndEmitDynamicWorkerCreationEvents(): void;
1726
1719
  /** @inheritDoc */
1727
- protected get type(): PoolType;
1720
+ protected shallCreateDynamicWorker(): boolean;
1728
1721
  /** @inheritDoc */
1729
1722
  protected get busy(): boolean;
1723
+ /** @inheritDoc */
1724
+ protected get type(): PoolType;
1730
1725
  }
1731
1726
 
1732
1727
  /**
@@ -1750,27 +1745,27 @@ declare class FixedThreadPool<Data = unknown, Response = unknown> extends Abstra
1750
1745
  */
1751
1746
  constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions, maximumNumberOfThreads?: number);
1752
1747
  /** @inheritDoc */
1753
- protected isMain(): boolean;
1748
+ protected checkAndEmitDynamicWorkerCreationEvents(): void;
1754
1749
  /** @inheritDoc */
1755
- protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1750
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1756
1751
  /** @inheritDoc */
1757
- protected sendStartupMessageToWorker(workerNodeKey: number): void;
1752
+ protected isMain(): boolean;
1753
+ /** @inheritDoc */
1754
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1758
1755
  /** @inheritDoc */
1759
1756
  protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1760
1757
  /** @inheritDoc */
1761
- protected registerOnceWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1758
+ protected sendStartupMessageToWorker(workerNodeKey: number): void;
1762
1759
  /** @inheritDoc */
1763
- protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1760
+ protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: readonly TransferListItem[]): void;
1764
1761
  /** @inheritDoc */
1765
1762
  protected shallCreateDynamicWorker(): boolean;
1766
1763
  /** @inheritDoc */
1767
- protected checkAndEmitDynamicWorkerCreationEvents(): void;
1764
+ protected get busy(): boolean;
1768
1765
  /** @inheritDoc */
1769
1766
  protected get type(): PoolType;
1770
1767
  /** @inheritDoc */
1771
1768
  protected get worker(): WorkerType;
1772
- /** @inheritDoc */
1773
- protected get busy(): boolean;
1774
1769
  }
1775
1770
 
1776
1771
  /**
@@ -1793,13 +1788,13 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1793
1788
  */
1794
1789
  constructor(min: number, max: number, filePath: string, opts?: ThreadPoolOptions);
1795
1790
  /** @inheritDoc */
1796
- protected shallCreateDynamicWorker(): boolean;
1797
- /** @inheritDoc */
1798
1791
  protected checkAndEmitDynamicWorkerCreationEvents(): void;
1799
1792
  /** @inheritDoc */
1800
- protected get type(): PoolType;
1793
+ protected shallCreateDynamicWorker(): boolean;
1801
1794
  /** @inheritDoc */
1802
1795
  protected get busy(): boolean;
1796
+ /** @inheritDoc */
1797
+ protected get type(): PoolType;
1803
1798
  }
1804
1799
 
1805
1800
  /**
@@ -1808,10 +1803,10 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1808
1803
  * @internal
1809
1804
  */
1810
1805
  declare class PriorityQueue<T> {
1811
- private head;
1812
- private tail;
1813
1806
  private readonly bucketSize;
1807
+ private head;
1814
1808
  private priorityEnabled;
1809
+ private tail;
1815
1810
  /** The priority queue maximum size. */
1816
1811
  maxSize: number;
1817
1812
  /**
@@ -1821,33 +1816,11 @@ declare class PriorityQueue<T> {
1821
1816
  * @returns PriorityQueue.
1822
1817
  */
1823
1818
  constructor(bucketSize?: number, enablePriority?: boolean);
1819
+ private getPriorityQueueNode;
1824
1820
  /**
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.
1821
+ * Clears the priority queue.
1849
1822
  */
1850
- enqueue(data: T, priority?: number): number;
1823
+ clear(): void;
1851
1824
  /**
1852
1825
  * Dequeue data from the priority queue.
1853
1826
  * @param bucket - The prioritized bucket to dequeue from.
@@ -1855,16 +1828,38 @@ declare class PriorityQueue<T> {
1855
1828
  */
1856
1829
  dequeue(bucket?: number): T | undefined;
1857
1830
  /**
1858
- * Clears the priority queue.
1831
+ * Enqueue data into the priority queue.
1832
+ * @param data - Data to enqueue.
1833
+ * @param priority - Priority of the data. Lower values have higher priority.
1834
+ * @returns The new size of the priority queue.
1859
1835
  */
1860
- clear(): void;
1836
+ enqueue(data: T, priority?: number): number;
1861
1837
  /**
1862
1838
  * Returns an iterator for the priority queue.
1863
1839
  * @returns An iterator for the priority queue.
1864
1840
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
1865
1841
  */
1866
1842
  [Symbol.iterator](): Iterator<T>;
1867
- private getPriorityQueueNode;
1843
+ /**
1844
+ * The number of filled prioritized buckets.
1845
+ * @returns The number of filled prioritized buckets.
1846
+ */
1847
+ get buckets(): number;
1848
+ /**
1849
+ * Whether priority is enabled.
1850
+ * @returns Whether priority is enabled.
1851
+ */
1852
+ get enablePriority(): boolean;
1853
+ /**
1854
+ * Enables/disables priority.
1855
+ * @param enablePriority - Whether to enable priority.
1856
+ */
1857
+ set enablePriority(enablePriority: boolean);
1858
+ /**
1859
+ * The priority queue size.
1860
+ * @returns The priority queue size.
1861
+ */
1862
+ get size(): number;
1868
1863
  }
1869
1864
 
1870
1865
  /**
@@ -1882,22 +1877,28 @@ interface FixedQueueNode<T> {
1882
1877
  * @internal
1883
1878
  */
1884
1879
  interface IFixedQueue<T> {
1880
+ /**
1881
+ * Returns an iterator for the fixed queue.
1882
+ * @returns An iterator for the fixed queue.
1883
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
1884
+ */
1885
+ [Symbol.iterator]: () => Iterator<T>;
1885
1886
  /** The fixed queue capacity. */
1886
1887
  readonly capacity: number;
1887
- /** The fixed queue size. */
1888
- readonly size: number;
1889
- /** The fixed queue node array. */
1890
- nodeArray: FixedQueueNode<T>[];
1888
+ /**
1889
+ * Clears the fixed queue.
1890
+ */
1891
+ clear: () => void;
1892
+ /**
1893
+ * Dequeue data from the fixed queue.
1894
+ * @returns The dequeued data or `undefined` if the fixed queue is empty.
1895
+ */
1896
+ dequeue: () => T | undefined;
1891
1897
  /**
1892
1898
  * Checks if the fixed queue is empty.
1893
1899
  * @returns `true` if the fixed queue is empty, `false` otherwise.
1894
1900
  */
1895
1901
  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
1902
  /**
1902
1903
  * Enqueue data into the fixed queue.
1903
1904
  * @param data - Data to enqueue.
@@ -1906,27 +1907,21 @@ interface IFixedQueue<T> {
1906
1907
  * @throws If the fixed queue is full.
1907
1908
  */
1908
1909
  enqueue: (data: T, priority?: number) => number;
1910
+ /**
1911
+ * Checks if the fixed queue is full.
1912
+ * @returns `true` if the fixed queue is full, `false` otherwise.
1913
+ */
1914
+ full: () => boolean;
1909
1915
  /**
1910
1916
  * Gets data from the fixed queue.
1911
1917
  * @param index - The index of the data to get.
1912
1918
  * @returns The data at the index or `undefined` if the fixed queue is empty or the index is out of bounds.
1913
1919
  */
1914
1920
  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>;
1921
+ /** The fixed queue node array. */
1922
+ nodeArray: FixedQueueNode<T>[];
1923
+ /** The fixed queue size. */
1924
+ readonly size: number;
1930
1925
  }
1931
1926
 
1932
1927
  /**
@@ -1942,30 +1937,47 @@ declare const availableParallelism: () => number;
1942
1937
  * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
1943
1938
  * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
1944
1939
  */
1945
- declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> {
1940
+ declare abstract class AbstractWorker<MainWorker extends MessagePort | Worker, Data = unknown, Response = unknown> {
1946
1941
  protected readonly isMain: boolean | undefined;
1947
1942
  private readonly mainWorker;
1948
1943
  protected opts: WorkerOptions;
1949
1944
  /**
1950
- * Worker id.
1945
+ * Handler id of the `activeInterval` worker activity check.
1951
1946
  */
1952
- protected abstract id: number;
1947
+ protected activeInterval?: NodeJS.Timeout;
1953
1948
  /**
1954
- * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
1949
+ * Worker id.
1955
1950
  */
1956
- protected taskFunctions: Map<string, TaskFunctionObject<Data, Response>>;
1951
+ protected abstract id: number;
1957
1952
  /**
1958
1953
  * Timestamp of the last task processed by this worker.
1959
1954
  */
1960
1955
  protected lastTaskTimestamp: number;
1956
+ /**
1957
+ * Runs the given task.
1958
+ * @param task - The task to execute.
1959
+ */
1960
+ protected readonly run: (task: Task<Data>) => void;
1961
+ /**
1962
+ * Runs the given task function asynchronously.
1963
+ * @param fn - Task function that will be executed.
1964
+ * @param task - Input data for the task function.
1965
+ */
1966
+ protected readonly runAsync: (fn: TaskAsyncFunction<Data, Response>, task: Task<Data>) => void;
1967
+ /**
1968
+ * Runs the given task function synchronously.
1969
+ * @param fn - Task function that will be executed.
1970
+ * @param task - Input data for the task function.
1971
+ */
1972
+ protected readonly runSync: (fn: TaskSyncFunction<Data, Response>, task: Task<Data>) => void;
1961
1973
  /**
1962
1974
  * Performance statistics computation requirements.
1963
1975
  */
1964
1976
  protected statistics?: WorkerStatistics;
1965
1977
  /**
1966
- * Handler id of the `activeInterval` worker activity check.
1978
+ * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
1967
1979
  */
1968
- protected activeInterval?: NodeJS.Timeout;
1980
+ protected taskFunctions: Map<string, TaskFunctionObject<Data, Response>>;
1969
1981
  /**
1970
1982
  * Constructs a new poolifier worker.
1971
1983
  * @param isMain - Whether this is the main worker or not.
@@ -1973,66 +1985,62 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
1973
1985
  * @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
1986
  * @param opts - Options for the worker.
1975
1987
  */
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;
1988
+ constructor(isMain: boolean | undefined, mainWorker: MainWorker | null | undefined, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1997
1989
  /**
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.
1990
+ * Returns the main worker.
1991
+ * @returns Reference to the main worker.
1992
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
2001
1993
  */
2002
- removeTaskFunction(name: string): TaskFunctionOperationResult;
1994
+ protected getMainWorker(): MainWorker;
2003
1995
  /**
2004
- * Lists the properties of the worker's task functions.
2005
- * @returns The properties of the worker's task functions.
1996
+ * Handles an error and convert it to a string so it can be sent back to the main worker.
1997
+ * @param error - The error raised by the worker.
1998
+ * @returns The error message.
2006
1999
  */
2007
- listTaskFunctionsProperties(): TaskFunctionProperties[];
2000
+ protected handleError(error: Error | string): string;
2008
2001
  /**
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.
2002
+ * Handles a kill message sent by the main worker.
2003
+ * @param message - The kill message.
2012
2004
  */
2013
- setDefaultTaskFunction(name: string): TaskFunctionOperationResult;
2005
+ protected handleKillMessage(message: MessageValue<Data>): void;
2014
2006
  /**
2015
2007
  * Handles the ready message sent by the main worker.
2016
2008
  * @param message - The ready message.
2017
2009
  */
2018
2010
  protected abstract handleReadyMessage(message: MessageValue<Data>): void;
2011
+ protected handleTaskFunctionOperationMessage(message: MessageValue<Data>): void;
2019
2012
  /**
2020
2013
  * Worker message listener.
2021
2014
  * @param message - The received message.
2022
2015
  */
2023
2016
  protected messageListener(message: MessageValue<Data>): void;
2024
- protected handleTaskFunctionOperationMessage(message: MessageValue<Data>): void;
2025
2017
  /**
2026
- * Handles a kill message sent by the main worker.
2027
- * @param message - The kill message.
2018
+ * Sends task functions properties to the main worker.
2028
2019
  */
2029
- protected handleKillMessage(message: MessageValue<Data>): void;
2020
+ protected sendTaskFunctionsPropertiesToMainWorker(): void;
2021
+ /**
2022
+ * Sends a message to main worker.
2023
+ * @param message - The response message.
2024
+ */
2025
+ protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
2026
+ private beginTaskPerformance;
2027
+ /**
2028
+ * Checks if the worker should be terminated, because its living too long.
2029
+ */
2030
+ private checkActive;
2030
2031
  /**
2031
2032
  * Check if the message worker id is set and matches the worker id.
2032
2033
  * @param message - The message to check.
2033
2034
  * @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
2035
  */
2035
2036
  private checkMessageWorkerId;
2037
+ /**
2038
+ * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
2039
+ * @param taskFunctions - The task function(s) parameter that should be checked.
2040
+ */
2041
+ private checkTaskFunctions;
2042
+ private checkWorkerOptions;
2043
+ private endTaskPerformance;
2036
2044
  /**
2037
2045
  * Starts the worker check active interval.
2038
2046
  */
@@ -2041,51 +2049,38 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
2041
2049
  * Stops the worker check active interval.
2042
2050
  */
2043
2051
  private stopCheckActive;
2052
+ private updateLastTaskTimestamp;
2044
2053
  /**
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.
2054
+ * Adds a task function to the worker.
2055
+ * If a task function with the same name already exists, it is replaced.
2056
+ * @param name - The name of the task function to add.
2057
+ * @param fn - The task function to add.
2058
+ * @returns Whether the task function was added or not.
2061
2059
  */
2062
- protected sendTaskFunctionsPropertiesToMainWorker(): void;
2060
+ addTaskFunction(name: string, fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>): TaskFunctionOperationResult;
2063
2061
  /**
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.
2062
+ * Checks if the worker has a task function with the given name.
2063
+ * @param name - The name of the task function to check.
2064
+ * @returns Whether the worker has a task function with the given name or not.
2067
2065
  */
2068
- protected handleError(error: Error | string): string;
2066
+ hasTaskFunction(name: string): TaskFunctionOperationResult;
2069
2067
  /**
2070
- * Runs the given task.
2071
- * @param task - The task to execute.
2068
+ * Lists the properties of the worker's task functions.
2069
+ * @returns The properties of the worker's task functions.
2072
2070
  */
2073
- protected readonly run: (task: Task<Data>) => void;
2071
+ listTaskFunctionsProperties(): TaskFunctionProperties[];
2074
2072
  /**
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.
2073
+ * Removes a task function from the worker.
2074
+ * @param name - The name of the task function to remove.
2075
+ * @returns Whether the task function existed and was removed or not.
2078
2076
  */
2079
- protected readonly runSync: (fn: TaskSyncFunction<Data, Response>, task: Task<Data>) => void;
2077
+ removeTaskFunction(name: string): TaskFunctionOperationResult;
2080
2078
  /**
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.
2079
+ * Sets the default task function to use in the worker.
2080
+ * @param name - The name of the task function to use as default task function.
2081
+ * @returns Whether the default task function was set or not.
2084
2082
  */
2085
- protected readonly runAsync: (fn: TaskAsyncFunction<Data, Response>, task: Task<Data>) => void;
2086
- private beginTaskPerformance;
2087
- private endTaskPerformance;
2088
- private updateLastTaskTimestamp;
2083
+ setDefaultTaskFunction(name: string): TaskFunctionOperationResult;
2089
2084
  }
2090
2085
 
2091
2086
  /**
@@ -2102,6 +2097,8 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
2102
2097
  * @since 2.0.0
2103
2098
  */
2104
2099
  declare class ClusterWorker<Data = unknown, Response = unknown> extends AbstractWorker<Worker, Data, Response> {
2100
+ /** @inheritDoc */
2101
+ protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2105
2102
  /**
2106
2103
  * Constructs a new poolifier cluster worker.
2107
2104
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
@@ -2112,8 +2109,6 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
2112
2109
  protected handleReadyMessage(message: MessageValue<Data>): void;
2113
2110
  /** @inheritDoc */
2114
2111
  protected get id(): number;
2115
- /** @inheritDoc */
2116
- protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2117
2112
  }
2118
2113
 
2119
2114
  /**
@@ -2130,6 +2125,8 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
2130
2125
  * @since 0.0.1
2131
2126
  */
2132
2127
  declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractWorker<MessagePort, Data, Response> {
2128
+ /** @inheritDoc */
2129
+ protected readonly sendToMainWorker: (message: MessageValue<Response>) => void;
2133
2130
  /**
2134
2131
  * Message port used to communicate with the main worker.
2135
2132
  */
@@ -2140,18 +2137,16 @@ declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractW
2140
2137
  * @param opts - Options for the worker.
2141
2138
  */
2142
2139
  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
2140
  /**
2152
2141
  * @inheritDoc
2153
2142
  */
2154
2143
  protected handleError(error: Error | string): string;
2144
+ /** @inheritDoc */
2145
+ protected handleKillMessage(message: MessageValue<Data>): void;
2146
+ /** @inheritDoc */
2147
+ protected handleReadyMessage(message: MessageValue<Data>): void;
2148
+ /** @inheritDoc */
2149
+ protected get id(): number;
2155
2150
  }
2156
2151
 
2157
2152
  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 };