From 3e10762eaf57880162df30798a86ff9693a7578a Mon Sep 17 00:00:00 2001 From: benjamin Date: Mon, 9 Mar 2026 17:44:45 +0100 Subject: [PATCH] optimize firebase remote reads --- .../presentation/remote/remote.component.ts | 3 ++- .../shows/services/show-song-data.service.ts | 24 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/app/modules/presentation/remote/remote.component.ts b/src/app/modules/presentation/remote/remote.component.ts index c40f404..e5db898 100644 --- a/src/app/modules/presentation/remote/remote.component.ts +++ b/src/app/modules/presentation/remote/remote.component.ts @@ -107,7 +107,8 @@ export class RemoteComponent implements OnDestroy { this.showSongs = list; this.show = show; const order = show?.order ?? []; - this.presentationSongs = order.map(id => presentationSongs.find(f => f.id === id) ?? null).filter((s): s is PresentationSong => !!s); + const presentationSongsById = new Map(presentationSongs.map(song => [song.id, song] as const)); + this.presentationSongs = order.map(id => presentationSongsById.get(id) ?? null).filter((s): s is PresentationSong => !!s); this.cRef.markForCheck(); }); diff --git a/src/app/modules/shows/services/show-song-data.service.ts b/src/app/modules/shows/services/show-song-data.service.ts index 5304fa0..d7e29e0 100644 --- a/src/app/modules/shows/services/show-song-data.service.ts +++ b/src/app/modules/shows/services/show-song-data.service.ts @@ -3,6 +3,7 @@ import {DbService} from '../../../services/db.service'; import {Observable} from 'rxjs'; import {ShowSong} from './show-song'; import {QueryFn} from '@angular/fire/compat/firestore/interfaces'; +import {shareReplay} from 'rxjs/operators'; @Injectable({ providedIn: 'root', @@ -10,10 +11,31 @@ import {QueryFn} from '@angular/fire/compat/firestore/interfaces'; export class ShowSongDataService { private collection = 'shows'; private subCollection = 'songs'; + private listCache = new Map>(); public constructor(private dbService: DbService) {} - public list$ = (showId: string, queryFn?: QueryFn): Observable => this.dbService.col$(`${this.collection}/${showId}/${this.subCollection}`, queryFn); + public list$ = (showId: string, queryFn?: QueryFn): Observable => { + if (queryFn) { + return this.dbService.col$(`${this.collection}/${showId}/${this.subCollection}`, queryFn); + } + + const cached = this.listCache.get(showId); + if (cached) { + return cached; + } + + const stream$ = this.dbService.col$(`${this.collection}/${showId}/${this.subCollection}`).pipe( + shareReplay({ + bufferSize: 1, + refCount: true, + }) + ); + + this.listCache.set(showId, stream$); + return stream$; + }; + public read$ = (showId: string, songId: string): Observable => this.dbService.doc$(`${this.collection}/${showId}/${this.subCollection}/${songId}`); public update$ = async (showId: string, songId: string, data: Partial): Promise => await this.dbService.doc(`${this.collection}/${showId}/${this.subCollection}/${songId}`).update(data);