X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2FWorkerFactory.ts;h=a5b401a3698417887c77426f843d0cae48bd61f0;hb=721646e902fa12d165d4a1da06fb963fb30dc9f2;hp=7e69fc56d974469ecfb6574f1a2c4246cad2cddc;hpb=a0239c1fdb0cebc4807aa2e381e7232cfe0e127c;p=e-mobility-charging-stations-simulator.git diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 7e69fc56..a5b401a3 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,14 +1,15 @@ -import { Worker, isMainThread } from 'worker_threads'; -import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker'; +import { type Worker, isMainThread } from 'node:worker_threads'; -import { PoolOptions } from 'poolifier'; -import type WorkerAbstract from './WorkerAbstract'; -import WorkerConstants from './WorkerConstants'; -import WorkerDynamicPool from './WorkerDynamicPool'; -import WorkerSet from './WorkerSet'; -import WorkerStaticPool from './WorkerStaticPool'; +import type { PoolOptions } from 'poolifier'; -export default class WorkerFactory { +import type { WorkerAbstract } from './WorkerAbstract'; +import { WorkerConstants } from './WorkerConstants'; +import { WorkerDynamicPool } from './WorkerDynamicPool'; +import { WorkerSet } from './WorkerSet'; +import { WorkerStaticPool } from './WorkerStaticPool'; +import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes'; + +export class WorkerFactory { private constructor() { // This is intentional } @@ -19,32 +20,30 @@ export default class WorkerFactory { workerOptions?: WorkerOptions ): WorkerAbstract | null { if (!isMainThread) { - throw new Error('Trying to get a worker implementation outside the main thread'); + throw new Error('Cannot get a worker implementation outside the main thread'); } - workerOptions = workerOptions ?? ({} as WorkerOptions); // why not default parameter ? + workerOptions = workerOptions ?? (WorkerConstants.EMPTY_OBJECT as WorkerOptions); workerOptions.workerStartDelay = - workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; // why null safety ? + workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; workerOptions.elementStartDelay = workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions); + workerOptions.poolOptions = + workerOptions?.poolOptions ?? (WorkerConstants.EMPTY_OBJECT as PoolOptions); workerOptions?.messageHandler && - // eslint-disable-next-line @typescript-eslint/no-misused-promises (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); - console.log('before'); - let workerImplementation: WorkerAbstract = null; // enabling strictNullChecks would be safer ? - console.log(workerImplementation); + let workerImplementation: WorkerAbstract | null = null; switch (workerProcessType) { - case WorkerProcessType.WORKER_SET: + case WorkerProcessType.workerSet: workerOptions.elementsPerWorker = workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; workerImplementation = new WorkerSet(workerScript, workerOptions); break; - case WorkerProcessType.STATIC_POOL: + case WorkerProcessType.staticPool: workerOptions.poolMaxSize = workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE; workerImplementation = new WorkerStaticPool(workerScript, workerOptions); break; - case WorkerProcessType.DYNAMIC_POOL: + case WorkerProcessType.dynamicPool: workerOptions.poolMinSize = workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE; workerOptions.poolMaxSize =