admin管理员组文章数量:1202595
I am new to the RxJs observables and I am finding difficulty in unsubscribing the loop once the criteria are met, can you please help me
In the below function if anytime the code reaches the if block I wanted to unsubscribe the Observables
Before asking this questions I have gone through below questions and none of them helped me much
- Rxjs observable wait until some condition is met
- AngularJs - RXJS Observable unsubscribe
Javascript Function
this.router.events.subscribe((url: any) => {
url = this.router.url;
if ((url === "/profile" || url === "/summary") && this.filterConfigFlag === false) {
/*
Perform operations
*/
} else {
console.log(typeof (url), url);
}
});
I am new to the RxJs observables and I am finding difficulty in unsubscribing the loop once the criteria are met, can you please help me
In the below function if anytime the code reaches the if block I wanted to unsubscribe the Observables
Before asking this questions I have gone through below questions and none of them helped me much
- Rxjs observable wait until some condition is met
- AngularJs - RXJS Observable unsubscribe
Javascript Function
this.router.events.subscribe((url: any) => {
url = this.router.url;
if ((url === "/profile" || url === "/summary") && this.filterConfigFlag === false) {
/*
Perform operations
*/
} else {
console.log(typeof (url), url);
}
});
Share
Improve this question
edited Jul 5, 2018 at 17:14
Mahesh G
asked Jul 5, 2018 at 17:12
Mahesh GMahesh G
1,2766 gold badges33 silver badges63 bronze badges
4
|
2 Answers
Reset to default 21You probably want takeWhile
:
this.router.events
.pipe(
takeWhile(url => url === ...), // when the condition isn't met it'll complete immediately
)
.subscribe(...);
but if you want to include also the last value (the value that completed the Observable) you'll need to do something like this:
Before RxJS 6.4
this.router.events
.pipe(
concatMap(url => url === ... ? of(url, null) : of(url)),
takeWhile(Boolean),
)
.subscribe(...);
Since RxJS 6.4
RxJS 6.4 added an overload to takeWhile
, it is now possible to include the last value like this:
this.router.events
.pipe(
takeWhile(url => url === ..., true),
)
.subscribe(...);
You may make use of a Subject
and takeUntil
operator as follows:
unsubscribe = new Subject<any>();
this.router.events.pipe(takeUntil(unsubscribe)).subscribe((url: any) => {
url = this.router.url;
if ((url === "/profile" || url === "/summary") && this.filterConfigFlag === false) {
/*
Perform operations
*/
this.unsubscribe.next();
} else {
console.log(typeof (url), url);
this.unsubscribe.next();
}
});
本文标签: javascriptHow to unsubscribe immediately after the the condition is met in RxJsStack Overflow
版权声明:本文标题:javascript - How to unsubscribe immediately after the the condition is met in RxJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738603181a2102190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
next
handler and complete the Observable right after that? – martin Commented Jul 5, 2018 at 17:13