X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=c0f4d2b9b1e655c80b8280eb60e92b29ffa17688;hb=d972af76b6d7d1d2a099d254eacf45245b5316ac;hp=890ac1fbf12892a04c7afcde3e27d04f09845154;hpb=d58b442097da31f8b974d51aef63c64470d9ab48;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index 890ac1fb..c0f4d2b9 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,4 +1,4 @@ -import fs from 'node:fs'; +import { type FSWatcher, readFileSync } from 'node:fs'; import type { ChargingStation } from './ChargingStation'; import { ChargingStationUtils } from './ChargingStationUtils'; @@ -7,7 +7,7 @@ import { Utils, handleFileException, logger, watchJsonFile } from '../utils'; type IdTagsCacheValueType = { idTags: string[]; - idTagsFileWatcher: fs.FSWatcher | undefined; + idTagsFileWatcher: FSWatcher | undefined; }; export class IdTagsCache { @@ -28,7 +28,7 @@ export class IdTagsCache { } /** - * Get one idtag from the cache given the distribution + * Gets one idtag from the cache given the distribution * Must be called after checking the cache is not an empty array * * @param distribution - @@ -56,7 +56,7 @@ export class IdTagsCache { } /** - * Get all idtags from the cache + * Gets all idtags from the cache * Must be called after checking the cache is not an empty array * * @param file - @@ -70,7 +70,7 @@ export class IdTagsCache { } public deleteIdTags(file: string): boolean { - return this.deleteIdTagsCache(file); + return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file); } private getRandomIdTag(hashId: string, file: string): string { @@ -155,12 +155,14 @@ export class IdTagsCache { return this.idTagsCaches.delete(file); } - private deleteIdTagsCacheIndexes(file: string): void { + private deleteIdTagsCacheIndexes(file: string): boolean { + let deleted: boolean[]; for (const [key] of this.idTagsCachesAddressableIndexes) { if (key.startsWith(file)) { - this.idTagsCachesAddressableIndexes.delete(key); + deleted.push(this.idTagsCachesAddressableIndexes.delete(key)); } } + return !deleted.some((value) => value === false); } private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string { @@ -170,7 +172,7 @@ export class IdTagsCache { private getIdTagsFromFile(file: string): string[] { if (Utils.isNotEmptyString(file)) { try { - return JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; + return JSON.parse(readFileSync(file, 'utf8')) as string[]; } catch (error) { handleFileException( file,