isUndefined,
logPrefix,
logger,
+ once,
roundTo,
secureRandom,
sleep,
private getStationInfoFromTemplate(): ChargingStationInfo {
const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile()!;
checkTemplate(stationTemplate, this.logPrefix(), this.templateFile);
- warnTemplateKeysDeprecation(stationTemplate, this.logPrefix(), this.templateFile);
+ const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this);
+ warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile);
if (stationTemplate?.Connectors) {
checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile);
}
import merge from 'just-merge';
import { Constants } from './Constants';
-import { hasOwnProp, isCFEnvironment, isNotEmptyString, isUndefined } from './Utils';
+import { hasOwnProp, isCFEnvironment, isNotEmptyString, isUndefined, once } from './Utils';
import {
ApplicationProtocol,
type ConfigurationData,
}
public static getStationTemplateUrls(): StationTemplateUrl[] | undefined {
- Configuration.checkDeprecatedConfigurationKeys();
+ const checkDeprecatedConfigurationKeysOnce = once(
+ Configuration.checkDeprecatedConfigurationKeys.bind(Configuration),
+ this,
+ );
+ checkDeprecatedConfigurationKeysOnce();
return Configuration.getConfigurationData()?.stationTemplateUrls;
}
}
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;
+ };
+};
isUndefined,
isValidTime,
logPrefix,
+ once,
promiseWithTimeout,
roundTo,
secureRandom,
isObject,
isUndefined,
isValidTime,
+ once,
roundTo,
secureRandom,
sleep,
expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
});
+
+ it('Verify once()', () => {
+ let called = 0;
+ const fn = () => ++called;
+ const onceFn = once(fn, this);
+ const result1 = onceFn();
+ expect(called).toBe(1);
+ expect(result1).toBe(1);
+ const result2 = onceFn();
+ expect(called).toBe(1);
+ expect(result2).toBe(1);
+ const result3 = onceFn();
+ expect(called).toBe(1);
+ expect(result3).toBe(1);
+ });
});