refactor: silence rollup warning
[poolifier.git] / src / utils.ts
index efd1f124084776667bd60495f202cec32c7e04a9..6f9d1226bb71210b020d987b32d169bbd593b5b8 100644 (file)
@@ -182,3 +182,27 @@ export const updateMeasurementStatistics = (
     }
   }
 }
+
+/**
+ * Executes a function once at a time.
+ *
+ * @param fn - The function to execute.
+ * @param context - The context to bind the function to.
+ * @returns The function to execute.
+ */
+export const once = (
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  fn: (...args: any[]) => void,
+  context: unknown
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+): ((...args: any[]) => void) => {
+  let called = false
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  return function (...args: any[]): void {
+    if (!called) {
+      called = true
+      fn.apply(context, args)
+      called = false
+    }
+  }
+}