[RxJS] Use takeUntil instead of manually unsubscribing from Observables
2017-05-29 14:07
477 查看
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.
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 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] Stopping a Stream with TakeUntil
- [RxJS] Use RxJS concatMap to map and concat high order observables
- Use Blanket.js instead of jscover
- [RxJS] Use RxJS mergeMap to map and merge high order observables
- Item 1: 尽可能的使用属性代替可访问的数据成员(Always Use Properties Instead of Accessible Data Members)
- Item 4: Use Conditional Attributes Instead of #if
- When to Use Delegates Instead of Interfaces
- Effective C# 1:Always Use Properties Instead of Accessible Data Members
- Effective C# Item 4: Use Conditional Attributes Instead of #if
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- Item 4: Use Conditional Attributes Instead of #if(Effective C#)
- Use parameters instead of string concatenation for forming SQL queries,用参数方式来生成sql语句,而不是用连接字符串的方式
- Unit Testing in Xcode 4 – use OCUnit and SenTest instead of GHUnit
- Convert a model from Maya to WebGL for use with THREE.js
- Item 1: Use Properties Instead of Accessible Data Members
- Know When to Use an Active Object Instead of a Mutex
- Use google.com instead of google.com.hk
- Use Conditional Attributes instead of #if - Effective C#学习笔记(4)
- Take use of the line printer in Soc
- An effective way to use pattern to instead of multiple if else statements