Ajout du cache sur les appels pour les stats

Et appel à l'API sur l'affichage de l'onglet
This commit is contained in:
Sébastien André
2021-05-02 12:50:47 +02:00
parent d020646831
commit 97e6153cef
15 changed files with 336 additions and 350 deletions

View File

@@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { tap } from 'rxjs/operators';
import { CacheApiKey } from '../models/cache-api-key.enum';
import { of } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class ServiceCacheApi {
private cache: Map<CacheApiKey, Observable<any>>;
constructor() {
this.cache = new Map<CacheApiKey, Observable<any>>();
}
public get<T>(key: CacheApiKey, callToApi: Observable<T>, withResetCache: boolean = false) : Observable<T> {
if (withResetCache === true) {
this.cache.delete(key);
}
const cached = this.cache.get(key);
if (cached) {
return cached;
} else {
return callToApi.pipe(
tap(event => {
this.cache.set(key, of(event));
}));
}
}
public delete(key: CacheApiKey) {
this.cache.delete(key);
}
}