refactor: convert WorkerUtils class static methods to constified arrow
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 26 May 2023 22:06:58 +0000 (00:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 26 May 2023 22:06:58 +0000 (00:06 +0200)
function

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/WorkerDynamicPool.ts
src/worker/WorkerSet.ts
src/worker/WorkerStaticPool.ts
src/worker/WorkerUtils.ts

index ff80bfcbdf5a082b885620e025fd335db3696947..c8a87ceb2cda6716efeaf11fd7e99d3a53225d64 100644 (file)
@@ -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<WorkerData> {
   private readonly pool: DynamicThreadPool<WorkerData>;
@@ -18,10 +18,10 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
   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<Worker>;
     this.workerOptions.poolOptions.exitHandler = (
-      this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler
+      this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
     ).bind(this) as ExitHandler<Worker>;
     this.workerOptions.poolOptions.messageHandler.bind(this);
     this.pool = new DynamicThreadPool<WorkerData>(
@@ -67,7 +67,6 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
   public async addElement(elementData: WorkerData): Promise<void> {
     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));
   }
 }
index 513efb506f7a8f6f5cddaba641ec8e771fc10955..e1fce2e62058a8ee6c5750a7e01a2d48891c164b 100644 (file)
@@ -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<WorkerData> {
   private readonly workerSet: Set<WorkerSetElement>;
@@ -58,7 +58,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     ++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<WorkerData> {
         this
       ) as MessageHandler<Worker>
     );
-    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 {
index de1f5b29cfb89aebc39240584f9ed045ee6b1efb..3786992a7743356f6ed302bf94ed835565062094 100644 (file)
@@ -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<WorkerData> {
   private readonly pool: FixedThreadPool<WorkerData>;
@@ -18,10 +18,10 @@ export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
   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<Worker>;
     this.workerOptions.poolOptions.exitHandler = (
-      this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler
+      this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
     ).bind(this) as ExitHandler<Worker>;
     this.workerOptions.poolOptions.messageHandler.bind(this);
     this.pool = new FixedThreadPool(
@@ -66,7 +66,6 @@ export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
   public async addElement(elementData: WorkerData): Promise<void> {
     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));
   }
 }
index 943de290800f0b1804e4366da5c2d76ab2d6f28e..368d64f20585e0dfd737748bb64a6306e0e12405 100644 (file)
@@ -1,25 +1,19 @@
 import chalk from 'chalk';
 
-export class WorkerUtils {
-  private constructor() {
-    // This is intentional
-  }
+export const sleep = async (milliSeconds: number): Promise<NodeJS.Timeout> => {
+  return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
+};
 
-  public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
-    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));
+};