From 5f742aac345f2eb8897c24651d00c7fb43dabbf8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 2 Sep 2023 20:58:17 +0200 Subject: [PATCH] feat: print deprecation warnings once MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit closes #630 Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 4 +++- src/utils/Configuration.ts | 8 ++++++-- src/utils/Utils.ts | 15 +++++++++++++++ src/utils/index.ts | 1 + test/utils/Utils.test.ts | 16 ++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) 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); + }); }); -- 2.34.1