X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAuthorizedTagsCache.ts;h=a2b180e330600b37873d7c37b0e01c52edf2e3c6;hb=29405d4fc27d01f8186482c0826b290152d027fe;hp=4a9da4ce67f3bfa6e0ca699395f971d1a42712fc;hpb=9d7484a4667898757b7c23be3dec7458c337cb84;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index 4a9da4ce..a2b180e3 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -1,31 +1,32 @@ +import fs from 'fs'; + import { FileType } from '../types/FileType'; import FileUtils from '../utils/FileUtils'; -import Utils from '../utils/Utils'; -import fs from 'fs'; import logger from '../utils/Logger'; +import Utils from '../utils/Utils'; export default class AuthorizedTagsCache { private static instance: AuthorizedTagsCache | null = null; private readonly tagsCaches: Map; - private readonly FSWatchers: Map; + private readonly FSWatchers: Map; private constructor() { this.tagsCaches = new Map(); - this.FSWatchers = new Map(); + this.FSWatchers = new Map(); } public static getInstance(): AuthorizedTagsCache { - if (!AuthorizedTagsCache.instance) { + if (AuthorizedTagsCache.instance === null) { AuthorizedTagsCache.instance = new AuthorizedTagsCache(); } return AuthorizedTagsCache.instance; } - public getAuthorizedTags(file: string): string[] { - if (!this.hasTags(file)) { + public getAuthorizedTags(file: string): string[] | undefined { + if (this.hasTags(file) === false) { this.setTags(file, this.getAuthorizedTagsFromFile(file)); // Monitor authorization file - !this.FSWatchers.has(file) && + this.FSWatchers.has(file) === false && this.FSWatchers.set( file, FileUtils.watchJsonFile( @@ -34,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); @@ -63,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); } @@ -71,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); } @@ -80,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); } @@ -99,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; }