poolifier 4.2.2 → 4.2.4

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