Extract selection strategies to classes (#176)
[poolifier.git] / src / pools / thread / dynamic.ts
1 import type { PoolOptions } from '../abstract-pool'
2 import type { ThreadWorkerWithMessageChannel } from './fixed'
3 import { FixedThreadPool } from './fixed'
4
5 /**
6 * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
7 *
8 * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
9 * When the maximum number of threads is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
10 *
11 * @template Data Type of data sent to the worker. This can only be serializable data.
12 * @template Response Type of response of execution. This can only be serializable data.
13 *
14 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
15 * @since 0.0.1
16 */
17 export class DynamicThreadPool<
18 Data = unknown,
19 Response = unknown
20 > extends FixedThreadPool<Data, Response> {
21 /**
22 * Constructs a new poolifier dynamic thread pool.
23 *
24 * @param min Minimum number of threads which are always active.
25 * @param max Maximum number of threads that can be created by this pool.
26 * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
27 * @param opts Options for this dynamic thread pool. Default: `{ maxTasks: 1000 }`
28 */
29 public constructor (
30 min: number,
31 public readonly max: number,
32 filePath: string,
33 opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
34 ) {
35 super(min, filePath, opts)
36 }
37
38 /** @inheritdoc */
39 public isDynamic (): boolean {
40 return true
41 }
42 }