Fix par fix
This commit is contained in:
@@ -1,26 +1,25 @@
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { HttpHeaders } from "@angular/common/http";
|
||||
|
||||
import { ConfigurationHelper } from './configuration-helper';
|
||||
import { ServiceCacheApi } from './service-cache-api.service';
|
||||
import { ConfigurationHelper } from "./configuration-helper";
|
||||
import { ServiceCacheApi } from "./service-cache-api.service";
|
||||
|
||||
export class BaseService {
|
||||
protected headers: HttpHeaders;
|
||||
protected apiUrl: string;
|
||||
protected serviceCacheApi : ServiceCacheApi;
|
||||
protected headers: HttpHeaders;
|
||||
protected apiUrl: string;
|
||||
protected serviceCacheApi: ServiceCacheApi;
|
||||
|
||||
constructor() {
|
||||
ConfigurationHelper.settings.subscribe(settings =>
|
||||
{
|
||||
if (settings != null) {
|
||||
let tmpApiUrl : string = settings.apiUrl;
|
||||
|
||||
this.headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': tmpApiUrl
|
||||
});
|
||||
this.apiUrl = tmpApiUrl + '/api';
|
||||
}
|
||||
constructor() {
|
||||
ConfigurationHelper.settings.subscribe((settings) => {
|
||||
if (settings != null) {
|
||||
let tmpApiUrl: string = settings.apiUrl;
|
||||
|
||||
this.headers = new HttpHeaders({
|
||||
"Access-Control-Allow-Origin": tmpApiUrl,
|
||||
});
|
||||
this.apiUrl = tmpApiUrl + "/api";
|
||||
}
|
||||
});
|
||||
|
||||
this.serviceCacheApi = new ServiceCacheApi();
|
||||
}
|
||||
this.serviceCacheApi = new ServiceCacheApi();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,28 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
import { CacheApiKey } from '../models/cache-api-key.enum';
|
||||
import { of } from 'rxjs';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { Observable } from "rxjs";
|
||||
import { tap } from "rxjs/operators";
|
||||
import { CacheApiKey } from "../models/cache-api-key.enum";
|
||||
import { of } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
@Injectable({ providedIn: "root" })
|
||||
export class ServiceCacheApi {
|
||||
private cache: Map<CacheApiKey, Observable<any>>;
|
||||
|
||||
constructor() {
|
||||
constructor() {
|
||||
this.cache = new Map<CacheApiKey, Observable<any>>();
|
||||
}
|
||||
|
||||
public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> {
|
||||
public get<T>(key: CacheApiKey, callToApi: Observable<T>): Observable<T> {
|
||||
const cached = this.cache.get(key);
|
||||
|
||||
if (cached) {
|
||||
return cached;
|
||||
} else {
|
||||
return callToApi.pipe(
|
||||
tap(event => {
|
||||
this.cache.set(key, of(event));
|
||||
}));
|
||||
tap((event) => {
|
||||
this.cache.set(key, of(event));
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +30,7 @@ export class ServiceCacheApi {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
|
||||
public getByKey<T>(key: CacheApiKey) : Observable<T> {
|
||||
public getByKey<T>(key: CacheApiKey): Observable<T> {
|
||||
return this.cache.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user