optimize song list load
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Song} from './song';
|
||||
import {Observable} from 'rxjs';
|
||||
import {BehaviorSubject, Observable} from 'rxjs';
|
||||
import {DbService} from '../../../services/db.service';
|
||||
import {map} from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -9,10 +10,14 @@ import {DbService} from '../../../services/db.service';
|
||||
export class SongDataService {
|
||||
private collection = 'songs';
|
||||
|
||||
public constructor(private dbService: DbService) {}
|
||||
public constructor(private dbService: DbService) {
|
||||
this.dbService.col$<Song>(this.collection).subscribe(_ => this.list$.next(_));
|
||||
}
|
||||
|
||||
public list$ = (): Observable<Song[]> => this.dbService.col$(this.collection);
|
||||
public read$ = (songId: string): Observable<Song | null> => this.dbService.doc$(this.collection + '/' + songId);
|
||||
public list$ = new BehaviorSubject<Song[]>([]);
|
||||
// public list$ = (): Observable<Song[]> => this.dbService.col$(this.collection);
|
||||
//public read$ = (songId: string): Observable<Song | null> => this.dbService.doc$(this.collection + '/' + songId);
|
||||
public read$ = (songId: string): Observable<Song | null> => this.list$.pipe(map(_ => _.find(s => s.id === songId) || null));
|
||||
public update$ = async (songId: string, data: Partial<Song>): Promise<void> => await this.dbService.doc(this.collection + '/' + songId).update(data);
|
||||
public add = async (data: Partial<Song>): Promise<string> => (await this.dbService.col(this.collection).add(data)).id;
|
||||
public delete = async (songId: string): Promise<void> => await this.dbService.doc(this.collection + '/' + songId).delete();
|
||||
|
||||
@@ -29,7 +29,7 @@ export class SongService {
|
||||
// importCCLI = (songs: Song[]) => this.updateFromCLI(songs);
|
||||
}
|
||||
|
||||
public list$ = (): Observable<Song[]> => this.songDataService.list$(); //.pipe(tap(_ => (this.list = _)));
|
||||
public list$ = (): Observable<Song[]> => this.songDataService.list$; //.pipe(tap(_ => (this.list = _)));
|
||||
public read$ = (songId: string): Observable<Song | null> => this.songDataService.read$(songId);
|
||||
public read = (songId: string): Promise<Song | null> => firstValueFrom(this.read$(songId));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user