chore: v2.6.17
[poolifier.git] / src / utils.ts
index 1b657bce22acaf49a39552f411f1bc4899834777..0becc5ed9aed9147cfaa5351d60a5e757ba0554b 100644 (file)
@@ -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 = <KB extends KillBehavior>(
 ): 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<unknown> => {
+  return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'
+}