X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=dd839bc491b622aa871b876424474d24abdfb44e;hb=5d1b49719459de479de5dabe173ceff38d6c4071;hp=f77d84a97e6694d2c22cbdc0dd0a5bb97ab69f89;hpb=ea4b5fd037c5fe09eb6a2810332ad6bed1b5bc7f;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index f77d84a9..dd839bc4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,8 @@ import { getRandomValues } from 'node:crypto' import * as os from 'node:os' +import type { TaskFunctionProperties } from './utility-types.js' +import type { TaskFunctionObject } from './worker/task-functions.js' import type { KillBehavior } from './worker/worker-options.js' /** @@ -198,21 +200,40 @@ export const max = (...args: number[]): number => * @param fn - The function to wrap. * @param context - The context to bind the function to. * @returns The wrapped function. + * + * @typeParam A - The function's arguments. + * @typeParam R - The function's return value. + * @typeParam C - The function's context. * @internal */ // eslint-disable-next-line @typescript-eslint/no-explicit-any -export const once = ( +export const once = >( fn: (...args: A) => R, - context: T + context: C ): ((...args: A) => R) => { let result: R return (...args: A) => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (fn != null) { - result = fn.apply(context, args) + result = fn.apply(context, args) ;(fn as unknown as undefined) = (context as unknown as undefined) = undefined } return result } } + +export const buildTaskFunctionProperties = ( + name: string, + taskFunctionObject: TaskFunctionObject | undefined +): TaskFunctionProperties => { + return { + name, + ...(taskFunctionObject?.priority != null && { + priority: taskFunctionObject.priority + }), + ...(taskFunctionObject?.strategy != null && { + strategy: taskFunctionObject.strategy + }) + } +}