简体   繁体   中英

RxJs - Can I use a BehaviorSubject with takeUntil?

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;
    }
}

Of course you can, simply skip the first, initial, value with skip() :

this.status2$
    .pipe(
        takeUntil(
           this.status1$.pipe(skip(1))
        ),
    )
    .subscribe((val: boolean) => {
         // I execute until status1$ emits
    }

Btw: As of RxJS >= 5.5 you might use pipe as in my example. Also you might name your observables with a $ at the end so it reads "status1stream"

You can do with filter as well.

const source = new Subject();
const destory$ = new BehaviorSubject<boolean>(false);
source
  .pipe(takeUntil(destory$.pipe(filter((v: boolean) => v))))
  .subscribe(resp => {
    console.log("resp", resp);
  });

source.next("hello");

source.next("hello2");
destory$.next(true);
destory$.complete();
source.next("hello 3 ");

You're after takeWhile : ( since takeUntil doesn't take a predicate ).

var bs = new Rx.BehaviorSubject<boolean>(false); //create beahviour subject
const source = Rx.Observable.interval(1000);     //create observable
// take from obs while , behaviour subject not emitting true
const example = source.takeWhile ((a)=>bs.value!=true); 
const subscribe = example.subscribe(val => console.log(val));

setTimeout(()=>bs.next(true),3000); //make the BehaviorSubject emit true and stop.

http://jsbin.com/yaditucija/1/edit?js,console

A BehaviorSubject will have an initial value. So as soon as your takeUntil internally subscribes to it, it will release its initial value. See (here)[ http://reactivex.io/documentation/subject.html]

When an observer subscribes to a BehaviorSubject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).

Use a PublishSubject instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM