optimize firebase monitor reads

This commit is contained in:
2026-03-09 17:48:59 +01:00
parent 3e10762eaf
commit ce9e5b5585

View File

@@ -1,15 +1,14 @@
import {ChangeDetectorRef, Component, OnInit} from '@angular/core'; import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {debounceTime, distinctUntilChanged, filter, map, switchMap, tap} from 'rxjs/operators'; import {debounceTime, distinctUntilChanged, filter, map, shareReplay, switchMap, takeUntil, tap} from 'rxjs/operators';
import {ShowService} from '../../shows/services/show.service'; import {ShowService} from '../../shows/services/show.service';
import {SongService} from '../../songs/services/song.service';
import {Song} from '../../songs/services/song'; import {Song} from '../../songs/services/song';
import {GlobalSettingsService} from '../../../services/global-settings.service'; import {GlobalSettingsService} from '../../../services/global-settings.service';
import {Config} from '../../../services/config'; import {Config} from '../../../services/config';
import {Observable} from 'rxjs'; import {Observable, Subject} from 'rxjs';
import {ConfigService} from '../../../services/config.service'; import {ConfigService} from '../../../services/config.service';
import {songSwitch} from '../../../widget-modules/components/song-text/animation'; import {songSwitch} from '../../../widget-modules/components/song-text/animation';
import {TextRenderingService} from '../../songs/services/text-rendering.service';
import {PresentationBackground, Show} from '../../shows/services/show'; import {PresentationBackground, Show} from '../../shows/services/show';
import {ShowSong} from '../../shows/services/show-song';
import {ShowSongService} from '../../shows/services/show-song.service'; import {ShowSongService} from '../../shows/services/show-song.service';
import {openFullscreen} from '../../../services/fullscreen'; import {openFullscreen} from '../../../services/fullscreen';
import {AsyncPipe, DatePipe, NgIf} from '@angular/common'; import {AsyncPipe, DatePipe, NgIf} from '@angular/common';
@@ -25,7 +24,7 @@ import {ShowTypePipe} from '../../../widget-modules/pipes/show-type-translater/s
animations: [songSwitch], animations: [songSwitch],
imports: [NgIf, LogoComponent, SongTextComponent, LegalComponent, AsyncPipe, DatePipe, ShowTypePipe], imports: [NgIf, LogoComponent, SongTextComponent, LegalComponent, AsyncPipe, DatePipe, ShowTypePipe],
}) })
export class MonitorComponent implements OnInit { export class MonitorComponent implements OnInit, OnDestroy {
public song: Song | null = null; public song: Song | null = null;
public zoom = 10; public zoom = 10;
public currentShowId: string | null = null; public currentShowId: string | null = null;
@@ -37,12 +36,11 @@ export class MonitorComponent implements OnInit {
public date: Date | null = null; public date: Date | null = null;
public config$: Observable<Config | null>; public config$: Observable<Config | null>;
public presentationBackground: PresentationBackground = 'none'; public presentationBackground: PresentationBackground = 'none';
private destroy$ = new Subject<void>();
public constructor( public constructor(
private showService: ShowService, private showService: ShowService,
private showSongService: ShowSongService, private showSongService: ShowSongService,
private songService: SongService,
private textRenderingService: TextRenderingService,
private globalSettingsService: GlobalSettingsService, private globalSettingsService: GlobalSettingsService,
private configService: ConfigService, private configService: ConfigService,
private cRef: ChangeDetectorRef private cRef: ChangeDetectorRef
@@ -52,40 +50,68 @@ export class MonitorComponent implements OnInit {
public ngOnInit(): void { public ngOnInit(): void {
openFullscreen(); openFullscreen();
this.globalSettingsService.get$ const currentShowId$ = this.globalSettingsService.get$
.pipe( .pipe(
debounceTime(100), debounceTime(100),
filter(_ => !!_), filter(_ => !!_),
map(_ => _), map(_ => _),
map(_ => _.currentShow), map(_ => _.currentShow),
distinctUntilChanged(), distinctUntilChanged(),
tap(_ => (this.currentShowId = _)) tap(_ => (this.currentShowId = _)),
) takeUntil(this.destroy$)
);
const show$ = currentShowId$
.pipe( .pipe(
switchMap(_ => this.showService.read$(_)), switchMap(showId => this.showService.read$(showId)),
filter(_ => !!_), filter((show): show is Show => !!show),
map(_ => _), shareReplay({
tap<Show>(_ => { bufferSize: 1,
this.showType = _.showType; refCount: true,
this.date = _.date.toDate(); }),
this.index = _.presentationSection; takeUntil(this.destroy$)
this.presentationBackground = _.presentationBackground; );
this.presentationDynamicCaption = _.presentationDynamicCaption;
this.presentationDynamicText = _.presentationDynamicText; show$
this.zoom = _.presentationZoom ?? 30; .pipe(
if (this.songId !== _.presentationSongId) this.songId = 'empty'; tap(show => {
this.showType = show.showType;
this.date = show.date.toDate();
this.index = show.presentationSection;
this.presentationBackground = show.presentationBackground;
this.presentationDynamicCaption = show.presentationDynamicCaption;
this.presentationDynamicText = show.presentationDynamicText;
this.zoom = show.presentationZoom ?? 30;
}),
takeUntil(this.destroy$)
)
.subscribe(() => this.cRef.markForCheck());
show$
.pipe(
map(show => ({showId: show.id, presentationSongId: show.presentationSongId})),
distinctUntilChanged((a, b) => a.showId === b.showId && a.presentationSongId === b.presentationSongId),
tap(({presentationSongId}) => {
if (this.songId !== presentationSongId) {
this.songId = 'empty';
}
setTimeout(() => { setTimeout(() => {
this.songId = _.presentationSongId; this.songId = presentationSongId;
this.cRef.markForCheck(); this.cRef.markForCheck();
}, 600); }, 600);
}), }),
switchMap((_: Show) => this.showSongService.read$(_.id, _.presentationSongId)), switchMap(({showId, presentationSongId}) => this.showSongService.read$(showId, presentationSongId)),
filter(_ => !!_), filter((song): song is ShowSong => !!song),
map(_ => _ as Song) takeUntil(this.destroy$)
) )
.subscribe(_ => { .subscribe(song => {
this.song = _; this.song = song;
this.cRef.markForCheck(); this.cRef.markForCheck();
}); });
} }
public ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
} }