Empty string handling fixes
[e-mobility-charging-stations-simulator.git] / src / charging-station / AuthorizedTagsCache.ts
index 747b7593e422d5ef123444170a6cbc470a8333b0..a2b180e330600b37873d7c37b0e01c52edf2e3c6 100644 (file)
@@ -8,11 +8,11 @@ import Utils from '../utils/Utils';
 export default class AuthorizedTagsCache {
   private static instance: AuthorizedTagsCache | null = null;
   private readonly tagsCaches: Map<string, string[]>;
-  private readonly FSWatchers: Map<string, fs.FSWatcher>;
+  private readonly FSWatchers: Map<string, fs.FSWatcher | undefined>;
 
   private constructor() {
     this.tagsCaches = new Map<string, string[]>();
-    this.FSWatchers = new Map<string, fs.FSWatcher>();
+    this.FSWatchers = new Map<string, fs.FSWatcher | undefined>();
   }
 
   public static getInstance(): AuthorizedTagsCache {
@@ -22,7 +22,7 @@ export default class AuthorizedTagsCache {
     return AuthorizedTagsCache.instance;
   }
 
-  public getAuthorizedTags(file: string): string[] {
+  public getAuthorizedTags(file: string): string[] | undefined {
     if (this.hasTags(file) === false) {
       this.setTags(file, this.getAuthorizedTagsFromFile(file));
       // Monitor authorization file
@@ -35,13 +35,10 @@ export default class AuthorizedTagsCache {
             file,
             null,
             (event, filename) => {
-              if (filename && event === 'change') {
+              if (!Utils.isEmptyString(filename) && event === 'change') {
                 try {
                   logger.debug(
-                    this.logPrefix(file) +
-                      ' ' +
-                      FileType.Authorization +
-                      ' file have changed, reload'
+                    `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
                   );
                   this.deleteTags(file);
                   this.deleteFSWatcher(file);
@@ -64,6 +61,10 @@ export default class AuthorizedTagsCache {
     return this.getTags(file);
   }
 
+  public deleteAuthorizedTags(file: string): boolean {
+    return this.deleteTags(file);
+  }
+
   private hasTags(file: string): boolean {
     return this.tagsCaches.has(file);
   }
@@ -72,7 +73,7 @@ export default class AuthorizedTagsCache {
     return this.tagsCaches.set(file, tags);
   }
 
-  private getTags(file: string): string[] {
+  private getTags(file: string): string[] | undefined {
     return this.tagsCaches.get(file);
   }
 
@@ -81,7 +82,7 @@ export default class AuthorizedTagsCache {
   }
 
   private deleteFSWatcher(file: string): boolean {
-    this.FSWatchers.get(file).close();
+    this.FSWatchers.get(file)?.close();
     return this.FSWatchers.delete(file);
   }
 
@@ -100,7 +101,7 @@ export default class AuthorizedTagsCache {
         );
       }
     } else {
-      logger.info(this.logPrefix(file) + ' No authorization file given)');
+      logger.info(`${this.logPrefix(file)} No authorization file given`);
     }
     return authorizedTags;
   }