I have a list of songs setup with Subject and Observable (shown with | async
in view), and now I want to delete a song off the list, do some filter()
and call next()
on the Subject.
How and where do I filter? Right now I am doing getValue()
on Subject and passing that to next()
on, well, Subject. This just seems wrong and circularish.
I also tried subscribing to the Subject and getting the data that way, filtering it and calling next()
inside subscribe()
, but I got a RangeError.
I could filter the Observable by storing all deleted id's. The Subject's list then becomes out of sync by having deleted songs on there and also every observer would have to have the deleted-id's-list which seems ludicrous. I'm rapidly growing old and mental. Please help me internet :(
export class ArtistComponent implements OnInit {
private repertoire$;
private repertoireSubject;
constructor(
private route: ActivatedRoute,
private service: ArtistService
) {
this.getRepertoire().subscribe(
songs => this.repertoireSubject.next(songs)
);
}
getRepertoire() {
return this.route.paramMap
.switchMap((params: ParamMap) =>
this.service.fetchRepertoire(params.get('id')));
}
//THIS IS WHERE I'M HAVING TROUBLE
delete(id): void {
this.repertoireSubject.next(
this.repertoireSubject.getValue().filter(song => (song.id !== id))
);
// TODO remove song from repertoire API
}
ngOnInit() {
this.repertoireSubject = new BehaviorSubject<any>(null);
this.repertoire$ = this.repertoireSubject.asObservable();
}
}