X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils.ts;h=0becc5ed9aed9147cfaa5351d60a5e757ba0554b;hb=206a35192f237de931242b95553ec8d2cf31b5d8;hp=1b657bce22acaf49a39552f411f1bc4899834777;hpb=5931725336d0de8a06146641ae6feef22bf60e6e;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index 1b657bce..0becc5ed 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,10 +1,15 @@ -import os from 'node:os' +import * as os from 'node:os' import type { MeasurementStatisticsRequirements, WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' import type { KillBehavior } from './worker/worker-options' +/** + * Default task name. + */ +export const DEFAULT_TASK_NAME = 'default' + /** * An intentional empty function. */ @@ -43,9 +48,9 @@ export const availableParallelism = (): number => { try { availableParallelism = os.availableParallelism() } catch { - const cpus = os.cpus() - if (Array.isArray(cpus) && cpus.length > 0) { - availableParallelism = cpus.length + const numberOfCpus = os.cpus() + if (Array.isArray(numberOfCpus) && numberOfCpus.length > 0) { + availableParallelism = numberOfCpus.length } } return availableParallelism @@ -111,3 +116,15 @@ export const isKillBehavior = ( ): value is KB => { return value === killBehavior } + +/** + * Detects whether the given value is an asynchronous function or not. + * + * @param fn - Any value. + * @returns `true` if `fn` was an asynchronous function, otherwise `false`. + */ +export const isAsyncFunction = ( + fn: unknown +): fn is (...args: unknown[]) => Promise => { + return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' +}