fix: fixes to pool initialization
[poolifier.git] / src / utils.ts
index d7ce5a210f2dea09290fd90a5f4eb64fcb2162d5..a0a9e530131cbe78df7401dff80452eef16e8634 100644 (file)
@@ -250,3 +250,27 @@ export const min = (...args: number[]): number =>
  */
 export const max = (...args: number[]): number =>
   args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity)
+
+/**
+ * Wraps a function so that it can only be called once.
+ *
+ * @param fn - The function to wrap.
+ * @param context - The context to bind the function to.
+ * @returns The wrapped function.
+ * @internal
+ */
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const once = <T, A extends any[], R>(
+  fn: (...args: A) => R,
+  context: T
+): ((...args: A) => R) => {
+  let result: R
+  return (...args: A) => {
+    if (fn != null) {
+      result = fn.apply<T, A, R>(context, args)
+      ;(fn as unknown as undefined) = (context as unknown as undefined) =
+        undefined
+    }
+    return result
+  }
+}