}
const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest;
if (
- !isNullOrUndefined(requestPayload?.hashIds) &&
- requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
+ !isNullOrUndefined(requestPayload.hashIds) &&
+ requestPayload.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
) {
return;
}
- if (!isNullOrUndefined(requestPayload?.hashId)) {
+ if (!isNullOrUndefined(requestPayload.hashId)) {
logger.error(
`${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`,
);
body ?? Constants.EMPTY_FREEZED_OBJECT,
),
)
- .then((protocolResponse: ProtocolResponse | undefined) => {
+ .then((protocolResponse?: ProtocolResponse) => {
if (!isNullOrUndefined(protocolResponse)) {
this.sendResponse(protocolResponse!);
}
this.uiServices
.get(version)
?.requestHandler(request)
- .then((protocolResponse: ProtocolResponse | undefined) => {
+ .then((protocolResponse?: ProtocolResponse) => {
if (!isNullOrUndefined(protocolResponse)) {
this.sendResponse(protocolResponse!);
}
import type { PoolEmitter, PoolInfo } from 'poolifier';
-import { WorkerConstants } from './WorkerConstants';
import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes';
import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils';
* @param workerScript -
* @param workerOptions -
*/
- constructor(
- workerScript: string,
- workerOptions: WorkerOptions = {
- workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
- elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
- poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
- poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
- elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
- poolOptions: {},
- },
- ) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
if (workerScript === null || workerScript === undefined) {
throw new Error('Worker script is not defined');
}
* @param workerScript -
* @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
this.pool = new DynamicThreadPool<WorkerData>(
this.workerOptions.poolMinSize,
import { isMainThread } from 'node:worker_threads';
-import type { ThreadPoolOptions } from 'poolifier';
-
import type { WorkerAbstract } from './WorkerAbstract';
import { WorkerConstants } from './WorkerConstants';
import { WorkerDynamicPool } from './WorkerDynamicPool';
import { WorkerStaticPool } from './WorkerStaticPool';
import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes';
+const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
+ workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
+ elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
+ poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
+ poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
+ elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
+ poolOptions: {},
+};
+
export class WorkerFactory {
private constructor() {
// This is intentional
if (!isMainThread) {
throw new Error('Cannot get a worker implementation outside the main thread');
}
- workerOptions = workerOptions ?? ({} as WorkerOptions);
- workerOptions.workerStartDelay =
- workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY;
- workerOptions.elementStartDelay =
- workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
- workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as ThreadPoolOptions);
+ workerOptions = { ...DEFAULT_WORKER_OPTIONS, ...workerOptions };
let workerImplementation: WorkerAbstract<T> | null = null;
switch (workerProcessType) {
case WorkerProcessType.workerSet:
- workerOptions.elementsPerWorker =
- workerOptions?.elementsPerWorker ?? WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
workerImplementation = new WorkerSet(workerScript, workerOptions);
break;
case WorkerProcessType.staticPool:
- workerOptions.poolMaxSize =
- workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
workerImplementation = new WorkerStaticPool(workerScript, workerOptions);
break;
case WorkerProcessType.dynamicPool:
- workerOptions.poolMinSize =
- workerOptions?.poolMinSize ?? WorkerConstants.DEFAULT_POOL_MIN_SIZE;
- workerOptions.poolMaxSize =
- workerOptions?.poolMaxSize ?? WorkerConstants.DEFAULT_POOL_MAX_SIZE;
workerImplementation = new WorkerDynamicPool(workerScript, workerOptions);
break;
default:
import { EventEmitter } from 'node:events';
import { SHARE_ENV, Worker } from 'node:worker_threads';
+import type { ThreadPoolOptions } from 'poolifier';
+
import { WorkerAbstract } from './WorkerAbstract';
import { WorkerConstants } from './WorkerConstants';
import {
} from './WorkerTypes';
import { sleep } from './WorkerUtils';
+const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = {
+ enableEvents: true,
+ restartWorkerOnError: true,
+};
+
export class WorkerSet extends WorkerAbstract<WorkerData> {
public readonly emitter!: EventEmitter;
private readonly workerSet: Set<WorkerSetElement>;
* @param workerScript -
* @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
this.workerOptions.poolOptions = {
- ...{
- enableEvents: true,
- restartWorkerOnError: true,
- },
+ ...DEFAULT_POOL_OPTIONS,
...this.workerOptions.poolOptions,
};
this.workerSet = new Set<WorkerSetElement>();
* @param workerScript -
* @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
this.pool = new FixedThreadPool(
this.workerOptions.poolMaxSize,