From 7d28dd57e8826de2efb33a36fed7e9de704a5938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eudes=20In=C3=A1cio?= Date: Tue, 9 Apr 2024 11:49:53 +0100 Subject: [PATCH] added a condition on dynamic bullets --- .../view-publications.page.html | 2 +- .../view-publications.page.ts | 2 + .../services/cache-service.service.spec.ts | 16 ++++++++ src/app/services/cache-service.service.ts | 39 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/app/services/cache-service.service.spec.ts create mode 100644 src/app/services/cache-service.service.ts diff --git a/src/app/pages/publications/view-publications/view-publications.page.html b/src/app/pages/publications/view-publications/view-publications.page.html index b97cb3dfe..de010ed35 100644 --- a/src/app/pages/publications/view-publications/view-publications.page.html +++ b/src/app/pages/publications/view-publications/view-publications.page.html @@ -65,7 +65,7 @@ --> -
+
{ + let service: CacheServiceService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(CacheServiceService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/cache-service.service.ts b/src/app/services/cache-service.service.ts new file mode 100644 index 000000000..96a026ec4 --- /dev/null +++ b/src/app/services/cache-service.service.ts @@ -0,0 +1,39 @@ +import { Injectable } from '@angular/core'; +import { BehaviorSubject } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class CacheService { + // A HashMap to store the cache. The key is the page and the value is the data. + private cache = new Map(); + // BehaviorSubject that will contain the updated cache data. + public cache$ = new BehaviorSubject(null); + + // The 'set' method for storing data in the cache. + set(key: string, data: any[]): void { + // We check if data already exists for this key. + if (this.cache.has(key)) { + // If it already exists, we throw an exception to prevent overwriting the data. + throw new Error(`Data already exists for key '${key}'. Use a different key or delete the existing one first.`); + } + // If there is no data for this key, we store it in the cache and update the BehaviorSubject. + this.cache.set(key, data); + this.cache$.next(this.cache.get(key)); + } + + // The 'get' method for retrieving data from the cache. + get(key: string): any[] { + // We retrieve the data from the cache and update the BehaviorSubject. + const data = this.cache.get(key); + this.cache$.next(data); + return data; + } + + // The 'clear' method to clear data from the cache. + clear(key: string): void { + // We remove the data from the cache and update the BehaviorSubject. + this.cache.delete(key); + this.cache$.next(null); + } +} \ No newline at end of file