I want to unsubscribe from another Observable using takeUntil using a BehaviorSubject. When I subscribe to the Observable with the takeUntil, it seems to immediately unsubscribe. This code works fine with a Subject, but I want an initial value set.
I'm using rxjs 5.5.6
//MyService1
class Observable1 {
status1: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
displayStatus1(val: boolean) {
this.status1.next(val)
}
}
//MyService2
class Observable2 {
status2: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
displayStatus2(val: boolean) {
this.status2.next(val)
}
}
//MyComponent
status: boolean;
constructor(private myService1: MyService1, private myService2: MyService2) {
this.subscribeToObservable1();
this.subscribeToObservable2();
}
subscribeToObservable1() {
this.myService1.status1.subscribe((val: boolean) => {
console.log('val: ', val);
}
}
subscribeToObservable2() {
this.myService2.status2
.takeUntil(this.myService1.status1)
.subscribe((val: boolean) => {
this.status = val;
}
}