More webapp changes+fixes
This commit is contained in:
58
webapp/src/app/core/services/key-cache.service.ts
Normal file
58
webapp/src/app/core/services/key-cache.service.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { Observable, of, map, shareReplay, catchError } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { AuthService } from './auth.service';
|
||||
import { KeyToken } from '../models';
|
||||
|
||||
export interface ResolvedKey {
|
||||
keyId: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class KeyCacheService {
|
||||
private apiService = inject(ApiService);
|
||||
private authService = inject(AuthService);
|
||||
|
||||
private keysCache$: Observable<Map<string, KeyToken>> | null = null;
|
||||
|
||||
resolveKey(keyId: string): Observable<ResolvedKey> {
|
||||
return this.getKeysMap().pipe(
|
||||
map(keysMap => {
|
||||
const key = keysMap.get(keyId);
|
||||
return {
|
||||
keyId,
|
||||
name: key?.name || keyId
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private getKeysMap(): Observable<Map<string, KeyToken>> {
|
||||
const userId = this.authService.getUserId();
|
||||
if (!userId) {
|
||||
return of(new Map());
|
||||
}
|
||||
|
||||
if (!this.keysCache$) {
|
||||
this.keysCache$ = this.apiService.getKeys(userId).pipe(
|
||||
map(response => {
|
||||
const map = new Map<string, KeyToken>();
|
||||
for (const key of response.keys) {
|
||||
map.set(key.keytoken_id, key);
|
||||
}
|
||||
return map;
|
||||
}),
|
||||
catchError(() => of(new Map())),
|
||||
shareReplay(1)
|
||||
);
|
||||
}
|
||||
return this.keysCache$;
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.keysCache$ = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user