refactor: factor out configuration section cache population
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 Jul 2023 11:41:42 +0000 (13:41 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 Jul 2023 11:41:42 +0000 (13:41 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/utils/Configuration.ts

index 2491757bea399797bfc7881b2f07ccaa19d94039..f01deceaf24836c2266020e9e8339991f08f5497 100644 (file)
@@ -60,33 +60,8 @@ export class Configuration {
   public static getConfigurationSection<T extends ConfigurationSectionType>(
     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(