I have a BehaviorSubject of pictures (because I need .getValue() in some cases), and an Observable representing the pictures marked as favorite, which is a subset of the pictures. My code works, and here it is :
public pictures: BehaviorSubject<Array<Picture>>;
public favoritesPictures: Observable<Array<Picture>>;
constructor(private http: Http) {
this.pictures = new BehaviorSubject([]);
this.favoritesPictures = Observable.create((observer) => {
this.pictures.subscribe((pictures) => {
observer.next(pictures.filter((p) => p.isFavorite));
});
});
}
However I feel like this syntax is quite "heavy", is there a cleaner way to bind an Observable as a subset of a BehaviorSubject ?
You can try following. BehaviorSubject is Observable.
public pictures: BehaviorSubject<Array<Picture>>;
public favoritesPictures: Observable<Array<Picture>>;
constructor(private http: Http) {
this.pictures = new BehaviorSubject([]);
this.favoritesPictures = this.pictures.map((a) => a.filter((p) => p.isFavorite));
}