feat: print deprecation warnings once
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 9b8074214a3440fee1201c40c297af2e15c6f784..58e9094c7a601bcd587cf59e6fd79b32a8823b95 100644 (file)
@@ -382,3 +382,18 @@ export const isArraySorted = <T>(array: T[], compareFn: (a: T, b: T) => number):
   }
   return true;
 };
+
+// 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) {
+      result = fn.apply<T, A, R>(context, args);
+      (fn as unknown as undefined) = (context as unknown as undefined) = undefined;
+    }
+    return result;
+  };
+};