42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { Injectable, inject } from '@angular/core';
|
|
import { Observable, of, catchError, map, shareReplay } from 'rxjs';
|
|
import { ApiService } from './api.service';
|
|
import { ClientPreview, UserPreview } from '../models';
|
|
|
|
export interface ResolvedClient {
|
|
clientId: string;
|
|
clientName: string | null;
|
|
userId: string;
|
|
userName: string | null;
|
|
agentModel: string;
|
|
agentVersion: string;
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ClientCacheService {
|
|
private apiService = inject(ApiService);
|
|
|
|
private cache = new Map<string, Observable<{client: ClientPreview, user: UserPreview} | null>>();
|
|
|
|
resolveClient(clientId: string): Observable<{client: ClientPreview, user: UserPreview} | null> {
|
|
if (!this.cache.has(clientId)) {
|
|
const request$ = this.apiService.getClientPreview(clientId).pipe(
|
|
map(response => ({
|
|
client: response.client,
|
|
user: response.user
|
|
})),
|
|
catchError(() => of(null)),
|
|
shareReplay(1)
|
|
);
|
|
this.cache.set(clientId, request$);
|
|
}
|
|
return this.cache.get(clientId)!;
|
|
}
|
|
|
|
clearCache(): void {
|
|
this.cache.clear();
|
|
}
|
|
}
|