From: Jérôme Benoit Date: Tue, 22 Aug 2023 21:47:27 +0000 (+0200) Subject: refactor: factor helper to run function once at a time X-Git-Tag: v2.6.32~9 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c3f0a07446e3751eed1df850525672f75562429f;p=poolifier.git refactor: factor helper to run function once at a time Signed-off-by: Jérôme Benoit --- 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 + } + } +}