From: Jérôme Benoit Date: Sat, 2 Sep 2023 18:58:17 +0000 (+0200) Subject: feat: print deprecation warnings once X-Git-Tag: v1.2.21~38 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5f742aac345f2eb8897c24651d00c7fb43dabbf8;p=e-mobility-charging-stations-simulator.git feat: print deprecation warnings once closes #630 Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f1e0e022..a8da873b 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -144,6 +144,7 @@ import { isUndefined, logPrefix, logger, + once, roundTo, secureRandom, sleep, @@ -1126,7 +1127,8 @@ export class ChargingStation { 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); } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index e9ec6884..4e908a5e 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; 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, @@ -75,7 +75,11 @@ export class Configuration { } public static getStationTemplateUrls(): StationTemplateUrl[] | undefined { - Configuration.checkDeprecatedConfigurationKeys(); + const checkDeprecatedConfigurationKeysOnce = once( + Configuration.checkDeprecatedConfigurationKeys.bind(Configuration), + this, + ); + checkDeprecatedConfigurationKeysOnce(); return Configuration.getConfigurationData()?.stationTemplateUrls; } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9b807421..58e9094c 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -382,3 +382,18 @@ export const isArraySorted = (array: T[], compareFn: (a: T, b: T) => number): } return true; }; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const once = ( + fn: (...args: A) => R, + context: T, +): ((...args: A) => R) => { + let result: R; + return (...args: A) => { + if (fn) { + result = fn.apply(context, args); + (fn as unknown as undefined) = (context as unknown as undefined) = undefined; + } + return result; + }; +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 5915ae77..f8fbe0e1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -49,6 +49,7 @@ export { isUndefined, isValidTime, logPrefix, + once, promiseWithTimeout, roundTo, secureRandom, diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 80aec47b..4a982775 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -26,6 +26,7 @@ import { isObject, isUndefined, isValidTime, + once, roundTo, secureRandom, sleep, @@ -431,4 +432,19 @@ describe('Utils test suite', () => { expect(isArraySorted([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false); expect(isArraySorted([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); + }); });