From 789871d6585093e2ab0444bd7ca063ca86c344f8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 May 2023 00:06:58 +0200 Subject: [PATCH] refactor: convert WorkerUtils class static methods to constified arrow function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/WorkerDynamicPool.ts | 9 ++++----- src/worker/WorkerSet.ts | 11 +++++------ src/worker/WorkerStaticPool.ts | 9 ++++----- src/worker/WorkerUtils.ts | 34 ++++++++++++++------------------- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index ff80bfcb..c8a87ceb 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -4,7 +4,7 @@ import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifie import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { WorkerUtils } from './WorkerUtils'; +import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; export class WorkerDynamicPool extends WorkerAbstract { private readonly pool: DynamicThreadPool; @@ -18,10 +18,10 @@ export class WorkerDynamicPool extends WorkerAbstract { constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); this.workerOptions.poolOptions.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler + this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler ).bind(this) as ErrorHandler; this.workerOptions.poolOptions.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler + this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler ).bind(this) as ExitHandler; this.workerOptions.poolOptions.messageHandler.bind(this); this.pool = new DynamicThreadPool( @@ -67,7 +67,6 @@ export class WorkerDynamicPool extends WorkerAbstract { public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay > 0 && - (await WorkerUtils.sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 513efb50..e1fce2e6 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -11,7 +11,7 @@ import { type WorkerOptions, type WorkerSetElement, } from './WorkerTypes'; -import { WorkerUtils } from './WorkerUtils'; +import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; export class WorkerSet extends WorkerAbstract { private readonly workerSet: Set; @@ -58,7 +58,7 @@ export class WorkerSet extends WorkerAbstract { ++this.getLastWorkerSetElement().numberOfWorkerElements; // Start element sequentially to optimize memory at startup if (this.workerOptions.elementStartDelay > 0) { - await WorkerUtils.sleep(this.workerOptions.elementStartDelay); + await sleep(this.workerOptions.elementStartDelay); } } @@ -94,15 +94,14 @@ export class WorkerSet extends WorkerAbstract { this ) as MessageHandler ); - worker.on('error', WorkerUtils.defaultErrorHandler.bind(this) as (err: Error) => void); + worker.on('error', defaultErrorHandler.bind(this) as (err: Error) => void); worker.on('exit', (code) => { - WorkerUtils.defaultExitHandler(code); + defaultExitHandler(code); this.workerSet.delete(this.getWorkerSetElementByWorker(worker)); }); this.workerSet.add({ worker, numberOfWorkerElements: 0 }); // Start worker sequentially to optimize memory at startup - this.workerOptions.workerStartDelay > 0 && - (await WorkerUtils.sleep(this.workerOptions.workerStartDelay)); + this.workerOptions.workerStartDelay > 0 && (await sleep(this.workerOptions.workerStartDelay)); } private getLastWorkerSetElement(): WorkerSetElement { diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index de1f5b29..3786992a 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -4,7 +4,7 @@ import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier' import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; -import { WorkerUtils } from './WorkerUtils'; +import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils'; export class WorkerStaticPool extends WorkerAbstract { private readonly pool: FixedThreadPool; @@ -18,10 +18,10 @@ export class WorkerStaticPool extends WorkerAbstract { constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); this.workerOptions.poolOptions.errorHandler = ( - this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler + this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler ).bind(this) as ErrorHandler; this.workerOptions.poolOptions.exitHandler = ( - this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler + this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler ).bind(this) as ExitHandler; this.workerOptions.poolOptions.messageHandler.bind(this); this.pool = new FixedThreadPool( @@ -66,7 +66,6 @@ export class WorkerStaticPool extends WorkerAbstract { public async addElement(elementData: WorkerData): Promise { await this.pool.execute(elementData); // Start element sequentially to optimize memory at startup - this.workerOptions.elementStartDelay > 0 && - (await WorkerUtils.sleep(this.workerOptions.elementStartDelay)); + this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay)); } } diff --git a/src/worker/WorkerUtils.ts b/src/worker/WorkerUtils.ts index 943de290..368d64f2 100644 --- a/src/worker/WorkerUtils.ts +++ b/src/worker/WorkerUtils.ts @@ -1,25 +1,19 @@ import chalk from 'chalk'; -export class WorkerUtils { - private constructor() { - // This is intentional - } +export const sleep = async (milliSeconds: number): Promise => { + return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds)); +}; - public static async sleep(milliSeconds: number): Promise { - return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds)); +export const defaultExitHandler = (code: number): void => { + if (code === 0) { + console.info(chalk.green('Worker exited successfully')); + } else if (code === 1) { + console.info(chalk.green('Worker terminated successfully')); + } else if (code > 1) { + console.error(chalk.red(`Worker exited with exit code: ${code.toString()}`)); } +}; - public static defaultExitHandler = (code: number): void => { - if (code === 0) { - console.info(chalk.green('Worker exited successfully')); - } else if (code === 1) { - console.info(chalk.green('Worker terminated successfully')); - } else if (code > 1) { - console.error(chalk.red(`Worker exited with exit code: ${code.toString()}`)); - } - }; - - public static defaultErrorHandler = (error: Error): void => { - console.error(chalk.red('Worker errored: ', error)); - }; -} +export const defaultErrorHandler = (error: Error): void => { + console.error(chalk.red('Worker errored: ', error)); +}; -- 2.34.1