From c3f0a07446e3751eed1df850525672f75562429f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 22 Aug 2023 23:47:27 +0200 Subject: [PATCH] refactor: factor helper to run function once at a time MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/worker-node.ts | 30 +++--------------------------- src/utils.ts | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 359dc26c..208acdab 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,7 +1,7 @@ import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' import type { Task } from '../utility-types' -import { DEFAULT_TASK_NAME } from '../utils' +import { DEFAULT_TASK_NAME, once } from '../utils' import { Deque } from '../deque' import { type IWorker, @@ -94,7 +94,7 @@ implements IWorkerNode { public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) if (this.onBackPressure != null && this.hasBackPressure()) { - this.once(this.onBackPressure)(this.info.id as number) + once(this.onBackPressure)(this.info.id as number) } return tasksQueueSize } @@ -103,7 +103,7 @@ implements IWorkerNode { public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) if (this.onBackPressure != null && this.hasBackPressure()) { - this.once(this.onBackPressure)(this.info.id as number) + once(this.onBackPressure)(this.info.id as number) } return tasksQueueSize } @@ -271,28 +271,4 @@ implements IWorkerNode { return worker.id } } - - /** - * Executes a function once at a time. - * - * @param fn - The function to execute. - * @param context - The context to bind the function to. - * @returns The function to execute. - */ - private once ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - fn: (...args: any[]) => void, - context = this - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ): (...args: any[]) => void { - let called = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return function (...args: any[]): void { - if (!called) { - called = true - fn.apply(context, args) - called = false - } - } - } } diff --git a/src/utils.ts b/src/utils.ts index efd1f124..7927c973 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -182,3 +182,27 @@ export const updateMeasurementStatistics = ( } } } + +/** + * Executes a function once at a time. + * + * @param fn - The function to execute. + * @param context - The context to bind the function to. + * @returns The function to execute. + */ +export const once = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + fn: (...args: any[]) => void, + context = this + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): ((...args: any[]) => void) => { + let called = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return function (...args: any[]): void { + if (!called) { + called = true + fn.apply(context, args) + called = false + } + } +} -- 2.34.1