X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=f38221692c8e76b06a57605f8a9129e198796cec;hb=05a852b826fb54cbab49a196bbd0d123c3d0c367;hp=a33758ce64c1ad72890b8d6e8b40b96336316c65;hpb=6c28572915241e7bdbe83d3d9896bc891dcf1f0b;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index a33758ce..f3822169 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,8 @@ -import * as os from 'node:os' 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' /** @@ -171,25 +174,31 @@ export const secureRandom = (): number => { /** * Returns the minimum of the given numbers. - * If no numbers are given, `Infinity` is returned. + * If no numbers are given, `Number.POSITIVE_INFINITY` is returned. * * @param args - The numbers to get the minimum of. * @returns The minimum of the given numbers. * @internal */ export const min = (...args: number[]): number => - args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) + args.reduce( + (minimum, num) => (minimum < num ? minimum : num), + Number.POSITIVE_INFINITY + ) /** * Returns the maximum of the given numbers. - * If no numbers are given, `-Infinity` is returned. + * If no numbers are given, `Number.NEGATIVE_INFINITY` is returned. * * @param args - The numbers to get the maximum of. * @returns The maximum of the given numbers. * @internal */ export const max = (...args: number[]): number => - args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) + args.reduce( + (maximum, num) => (maximum > num ? maximum : num), + Number.NEGATIVE_INFINITY + ) /** * Wraps a function so that it can only be called once. @@ -197,21 +206,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 + }) + } +}