X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerAbstract.ts;h=9a10e6f2afa1de4c463dd8a2803917b61f5b76b5;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=e78216def1d2aff36ce53572b30044a109675c28;hpb=a6ef1ece74c0d08e86a905571f4f6045c28131cb;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index e78216de..9a10e6f2 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,18 +1,17 @@ -import type { EventEmitterAsyncResource } from 'node:events'; -import { existsSync } from 'node:fs'; +import type { EventEmitterAsyncResource } from 'node:events' +import { existsSync } from 'node:fs' -import type { PoolInfo } from 'poolifier'; +import type { PoolInfo } from 'poolifier' -import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js'; -import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils.js'; +import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js' export abstract class WorkerAbstract { - protected readonly workerScript: string; - protected readonly workerOptions: WorkerOptions; - public abstract readonly info: PoolInfo | SetInfo; - public abstract readonly size: number; - public abstract readonly maxElementsPerWorker: number | undefined; - public abstract readonly emitter: EventEmitterAsyncResource | undefined; + protected readonly workerScript: string + protected readonly workerOptions: WorkerOptions + public abstract readonly info: PoolInfo | SetInfo + public abstract readonly size: number + public abstract readonly maxElementsPerWorker: number | undefined + public abstract readonly emitter: EventEmitterAsyncResource | undefined /** * `WorkerAbstract` constructor. @@ -20,39 +19,35 @@ export abstract class WorkerAbstract { * @param workerScript - * @param workerOptions - */ - constructor(workerScript: string, workerOptions: WorkerOptions) { + constructor (workerScript: string | undefined, workerOptions: WorkerOptions) { if (workerScript == null) { - throw new TypeError('Worker script is not defined'); + throw new TypeError('Worker script is not defined') } if (typeof workerScript !== 'string') { - throw new TypeError('Worker script must be a string'); + throw new TypeError('Worker script must be a string') } if (workerScript.trim().length === 0) { - throw new Error('Worker script is an empty string'); + throw new Error('Worker script is an empty string') } if (!existsSync(workerScript)) { - throw new Error('Worker script file does not exist'); + throw new Error('Worker script file does not exist') } - this.workerScript = workerScript; - this.workerOptions = workerOptions; - this.workerOptions.poolOptions!.errorHandler = - this.workerOptions.poolOptions?.errorHandler ?? defaultErrorHandler; - this.workerOptions.poolOptions!.exitHandler = - this.workerOptions.poolOptions?.exitHandler ?? defaultExitHandler; + this.workerScript = workerScript + this.workerOptions = workerOptions } /** * Starts the worker pool/set. */ - public abstract start(): Promise; + public abstract start (): Promise /** * Stops the worker pool/set. */ - public abstract stop(): Promise; + public abstract stop (): Promise /** * Adds a task element to the worker pool/set. * * @param elementData - */ - public abstract addElement(elementData: T): Promise; + public abstract addElement (elementData: T): Promise }