-/**
- * Base class containing some shared logic for all poolifier pools.
- *
- * @template Worker Type of worker which manages this pool.
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
- */
-export abstract class AbstractPool<
- Worker extends IWorker,
- Data = unknown,
- Response = unknown
-> implements IPool<Data, Response> {
- /**
- * List of currently available workers.
- */
- public readonly workers: Worker[] = []
+ this.setupHook()
+
+ for (let i = 1; i <= this.numberOfWorkers; i++) {
+ this.createAndSetupWorker()
+ }
+
+ if (this.opts.enableEvents === true) {
+ this.emitter = new PoolEmitter()
+ }
+ this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext<
+ Worker,
+ Data,
+ Response
+ >(
+ this,
+ this.opts.workerChoiceStrategy,
+ this.opts.workerChoiceStrategyOptions
+ )
+ }
+
+ private checkFilePath (filePath: string): void {
+ if (
+ filePath == null ||
+ (typeof filePath === 'string' && filePath.trim().length === 0)
+ ) {
+ throw new Error('Please specify a file with a worker implementation')
+ }
+ }
+
+ private checkNumberOfWorkers (numberOfWorkers: number): void {
+ if (numberOfWorkers == null) {
+ throw new Error(
+ 'Cannot instantiate a pool without specifying the number of workers'
+ )
+ } else if (!Number.isSafeInteger(numberOfWorkers)) {
+ throw new TypeError(
+ 'Cannot instantiate a pool with a non integer number of workers'
+ )
+ } else if (numberOfWorkers < 0) {
+ throw new RangeError(
+ 'Cannot instantiate a pool with a negative number of workers'
+ )
+ } else if (this.type === PoolType.FIXED && numberOfWorkers === 0) {
+ throw new Error('Cannot instantiate a fixed pool with no worker')
+ }
+ }
+
+ private checkPoolOptions (opts: PoolOptions<Worker>): void {
+ this.opts.workerChoiceStrategy =
+ opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
+ this.opts.workerChoiceStrategyOptions =
+ opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ this.opts.enableEvents = opts.enableEvents ?? true
+ this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
+ if (this.opts.enableTasksQueue) {
+ this.checkValidTasksQueueOptions(
+ opts.tasksQueueOptions as TasksQueueOptions
+ )
+ this.opts.tasksQueueOptions = this.buildTasksQueueOptions(
+ opts.tasksQueueOptions as TasksQueueOptions
+ )
+ }
+ }
+
+ private checkValidWorkerChoiceStrategy (
+ workerChoiceStrategy: WorkerChoiceStrategy
+ ): void {
+ if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
+ throw new Error(
+ `Invalid worker choice strategy '${workerChoiceStrategy}'`
+ )
+ }
+ }
+
+ private checkValidTasksQueueOptions (
+ tasksQueueOptions: TasksQueueOptions
+ ): void {
+ if ((tasksQueueOptions?.concurrency as number) <= 0) {
+ throw new Error(
+ `Invalid worker tasks concurrency '${
+ tasksQueueOptions.concurrency as number
+ }'`
+ )
+ }
+ }
+
+ /** @inheritDoc */
+ public abstract get type (): PoolType
+
+ /** @inheritDoc */
+ public abstract get size (): number