From: Jérôme Benoit Date: Thu, 27 Jul 2023 11:41:42 +0000 (+0200) Subject: refactor: factor out configuration section cache population X-Git-Tag: v1.2.20~116 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=81b9a105a722ddb59ceaec77f9131695d80be495;p=e-mobility-charging-stations-simulator.git refactor: factor out configuration section cache population Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 2491757b..f01decea 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -60,33 +60,8 @@ export class Configuration { public static getConfigurationSection( sectionName: ConfigurationSection, ): T { - if (!Configuration.configurationSectionCache.has(sectionName)) { - switch (sectionName) { - case ConfigurationSection.log: - Configuration.configurationSectionCache.set(sectionName, Configuration.buildLogSection()); - break; - case ConfigurationSection.performanceStorage: - Configuration.configurationSectionCache.set( - sectionName, - Configuration.buildPerformanceStorageSection(), - ); - break; - case ConfigurationSection.worker: - Configuration.configurationSectionCache.set( - sectionName, - Configuration.buildWorkerSection(), - ); - break; - case ConfigurationSection.uiServer: - Configuration.configurationSectionCache.set( - sectionName, - Configuration.buildUIServerSection(), - ); - break; - default: - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - throw new Error(`Unknown configuration section '${sectionName}'`); - } + if (!Configuration.isConfigurationSectionCached(sectionName)) { + Configuration.cacheConfigurationSection(sectionName); } return Configuration.configurationSectionCache.get(sectionName) as T; } @@ -188,6 +163,39 @@ export class Configuration { return Configuration.buildWorkerSection().processType === WorkerProcessType.dynamicPool; } + private static isConfigurationSectionCached(sectionName: ConfigurationSection): boolean { + return Configuration.configurationSectionCache.has(sectionName); + } + + private static cacheConfigurationSection(sectionName: ConfigurationSection): void { + switch (sectionName) { + case ConfigurationSection.log: + Configuration.configurationSectionCache.set(sectionName, Configuration.buildLogSection()); + break; + case ConfigurationSection.performanceStorage: + Configuration.configurationSectionCache.set( + sectionName, + Configuration.buildPerformanceStorageSection(), + ); + break; + case ConfigurationSection.worker: + Configuration.configurationSectionCache.set( + sectionName, + Configuration.buildWorkerSection(), + ); + break; + case ConfigurationSection.uiServer: + Configuration.configurationSectionCache.set( + sectionName, + Configuration.buildUIServerSection(), + ); + break; + default: + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Unknown configuration section '${sectionName}'`); + } + } + private static buildUIServerSection(): UIServerConfiguration { if (hasOwnProp(Configuration.getConfigurationData(), 'uiWebSocketServer')) { console.error(