Ajout du cache sur les appels pour les stats
Et appel à l'API sur l'affichage de l'onglet
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user