From 4a5cb830cd7a76257dd07a6c5dbf391eaf73e454 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Fri, 20 Dec 2024 13:56:25 -0500 Subject: [PATCH] caching works --- .../stillbox/src/app/calls/calls.component.ts | 2 +- .../src/app/talkgroups/talkgroups.service.ts | 57 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/client/stillbox/src/app/calls/calls.component.ts b/client/stillbox/src/app/calls/calls.component.ts index 80eadb7..46a5942 100644 --- a/client/stillbox/src/app/calls/calls.component.ts +++ b/client/stillbox/src/app/calls/calls.component.ts @@ -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'; } } diff --git a/client/stillbox/src/app/talkgroups/talkgroups.service.ts b/client/stillbox/src/app/talkgroups/talkgroups.service.ts index e9be1ab..d7c80b6 100644 --- a/client/stillbox/src/app/talkgroups/talkgroups.service.ts +++ b/client/stillbox/src/app/talkgroups/talkgroups.service.ts @@ -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>(); - constructor(private http: HttpClient) {} + private readonly _getTalkgroup = new Map>(); + private tgs$ = new Observable(); + constructor(private http: HttpClient) { + this.tgs$ = this.getTalkgroups().pipe(shareReplay(1)); + this.fillTgMap(); + } getTalkgroups(): Observable { return this.http.get('/api/talkgroup/'); } + tgKey(sys: number, tg: number): string { + return sys + ':' + tg; + } + getTalkgroupsPag(pagination: Pagination): Observable { return this.http.post('/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).next(tg); + } else { + const bs = new ReplaySubject(1); + bs.next(tg); + this._getTalkgroup.set(tgid, bs); + } + }); + }); + } getTalkgroup(sys: number, tg: number): Observable { - let tgid = { sys: sys, tg: tg }; - if (!this._getTalkgroup.get(tgid)) { - this._getTalkgroup.set( - tgid, - this.http - .get(`/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 { @@ -71,7 +102,7 @@ export class TalkgroupService { } putTalkgroup(tu: TalkgroupUpdate): Observable { - let tgid = { sys: tu.system_id, tg: tu.tgid }; + let tgid = this.tgKey(tu.system_id, tu.tgid); this._getTalkgroup.set( tgid, this.http