caching works

This commit is contained in:
Daniel Ponte 2024-12-20 13:56:25 -05:00
parent ab52890706
commit 4a5cb830cd
2 changed files with 45 additions and 14 deletions

View file

@ -94,7 +94,7 @@ export class DurationPipe implements PipeTransform {
transform(call: CallRecord, args?: any): string {
const seconds = call.duration / 1000;
return seconds.toFixed(2)+"s";
return seconds.toFixed(2) + 's';
}
}

View file

@ -1,6 +1,16 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable, share, shareReplay } from 'rxjs';
import {
BehaviorSubject,
concat,
map,
filter,
concatMap,
Observable,
ReplaySubject,
share,
shareReplay,
} from 'rxjs';
import { Talkgroup, TalkgroupUpdate, TGID } from '../talkgroup';
export interface Pagination {
@ -18,29 +28,50 @@ export interface TalkgroupsPaginated {
providedIn: 'root',
})
export class TalkgroupService {
private readonly _getTalkgroup = new Map<TGID, Observable<Talkgroup>>();
constructor(private http: HttpClient) {}
private readonly _getTalkgroup = new Map<string, Observable<Talkgroup>>();
private tgs$ = new Observable<Talkgroup[]>();
constructor(private http: HttpClient) {
this.tgs$ = this.getTalkgroups().pipe(shareReplay(1));
this.fillTgMap();
}
getTalkgroups(): Observable<Talkgroup[]> {
return this.http.get<Talkgroup[]>('/api/talkgroup/');
}
tgKey(sys: number, tg: number): string {
return sys + ':' + tg;
}
getTalkgroupsPag(pagination: Pagination): Observable<TalkgroupsPaginated> {
return this.http.post<TalkgroupsPaginated>('/api/talkgroup/', pagination);
}
fillTgMap() {
this.tgs$.subscribe((tgs) => {
tgs.forEach((tg, i, a) => {
let tgid = this.tgKey(tg.system_id, tg.tgid);
const rs = this._getTalkgroup.get(tgid);
if (rs) {
(rs as ReplaySubject<Talkgroup>).next(tg);
} else {
const bs = new ReplaySubject<Talkgroup>(1);
bs.next(tg);
this._getTalkgroup.set(tgid, bs);
}
});
});
}
getTalkgroup(sys: number, tg: number): Observable<Talkgroup> {
let tgid = <TGID>{ sys: sys, tg: tg };
if (!this._getTalkgroup.get(tgid)) {
this._getTalkgroup.set(
tgid,
this.http
.get<Talkgroup>(`/api/talkgroup/${sys}/${tg}`)
.pipe(shareReplay()),
const key = this.tgKey(sys, tg);
if (!this._getTalkgroup.get(key)) {
return this.tgs$.pipe(
concatMap((talkg) =>
talkg.filter((tgv) => tgv.tgid == tg && tgv.system_id == sys),
),
);
}
return this._getTalkgroup.get(tgid)!;
return this._getTalkgroup.get(key)!;
}
importRR(sysID: number, content: string): Observable<Talkgroup[]> {
@ -71,7 +102,7 @@ export class TalkgroupService {
}
putTalkgroup(tu: TalkgroupUpdate): Observable<Talkgroup> {
let tgid = <TGID>{ sys: tu.system_id, tg: tu.tgid };
let tgid = this.tgKey(tu.system_id, tu.tgid);
this._getTalkgroup.set(
tgid,
this.http