[英]RxJs - Can I use a BehaviorSubject with takeUntil?
I want to unsubscribe from another Observable using takeUntil using a BehaviorSubject.我想使用 BehaviorSubject 使用 takeUntil 取消订阅另一个 Observable。 When I subscribe to the Observable with the takeUntil, it seems to immediately unsubscribe.
当我使用 takeUntil 订阅 Observable 时,它似乎立即取消订阅。 This code works fine with a Subject, but I want an initial value set.
此代码适用于主题,但我想要一个初始值集。
I'm using rxjs 5.5.6我正在使用 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() : 当然,您可以使用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. 顺便说一句:从RxJS> = 5.5开始,您可以在我的示例中使用管道 。 Also you might name your observables with a
$
at the end so it reads "status1stream" 另外,您可能会在结尾处用
$
命名您的可观察对象,因此它显示为“ 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 ). 您正在
takeWhile
:之后( 因为takeUntil不带谓词 )。
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 http://jsbin.com/yaditucija/1/edit?js,控制台
A BehaviorSubject will have an initial value. BehaviorSubject将具有初始值。 So as soon as your
takeUntil
internally subscribes to it, it will release its initial value. 因此,只要您的
takeUntil
内部订阅它,它就会释放其初始值。 See (here)[ http://reactivex.io/documentation/subject.html] 参见(此处)[ 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. 请改用
PublishSubject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.