首页 > 代码库 > [RxJS] Use takeUntil instead of manually unsubscribing from Observables
[RxJS] Use takeUntil instead of manually unsubscribing from Observables
Manually unsubscribing from subscriptions is safe, but tedious and error-prone. This lesson will teach us about the takeUntil operator and its utility to make unsubscribing automatic.
const click$ = Rx.Observable.fromEvent(document, ‘click‘);const sub = click$.subscribe(function(ev) { console.log(ev.clientX);});setTimeout(() => { sub.unsubscribe();}, 2000);
In the code we manully unsubscribe.
We can use tha helper methods such as takeUntil, take() help to automatically handle subscritpiton.
const click$ = Rx.Observable .fromEvent(document, ‘click‘);const four$ = Rx.Observable.interval(4000).take(1);/*click$ --c------c---c-c-----c---c---c-four$ -----------------0|clickUntilFour$ --c------c---c-c-|*/const clickUntilFour$ = click$.takeUntil(four$);clickUntilFour$.subscribe(function (ev) { console.log(ev.clientX);});
[RxJS] Use takeUntil instead of manually unsubscribing from Observables
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。