[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);
});
时间: 2024-07-31 11:46:53

[RxJS] Use takeUntil instead of manually unsubscribing from Observables的相关文章

RxJS过滤数据流操作符学习 (Filtering Operators)

1. 过滤类操作符的模式 很多时候,上游Observable对象吐出的数据,并不是下游关心的,这个时候我们需要把不需要的数据,过滤掉.在RxJS中,提供了这类过滤操作符来实现这种功能. 过滤类操作符最基本的功能就是对一个给定的数据流中的每个数据判断是否满足某个条件,如果满足就传递给下游,如果不满足就抛弃. 判断一个数据是否有资格进入下游,是根据 "判定函数"的返回值, true代表可以进入下游,否则就会被淘汰. 有的过滤类操作符还可以接受一个函数参数"结果选择器",

构建流式应用—RxJS详解

讲之前先说一点哈,插入的图片,不能正常显示,图片一半被遮盖了,如果想看,鼠标右击然后图片地址,图片另存为去看.如果有知道怎么解决的,可以在下面给我评论,我会及时的修改. 好啦,开始: 目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observer RxJS · Operators Operators ·入门 一系列的 Operators 操作 使用 RxJS

响应式编程介绍

响应式编程简介 (原文) 你应该对响应式编程这个新事件有点好奇吧,尤其是与之相关的部分框架:Rx.Bacon.js.RAC等等. 在缺乏好的资源的情况下,学习响应式编程成为痛苦.我开始学的时候,做死地找各种教程.结果发现有用的只是极少部分,而且这少部分也只是表面上的东西,对于整个体系结构的理解也起不了多大的作用.直接去看那些库文档同样也理解不了.比如下面这个: Rx.Observable.prototype.flatMapLatest(selector, [thisArg]) Projects

[RxJS] Stopping a Stream with TakeUntil

Observables often need to be stopped before they are completed. This lesson shows how to use takeUntil to stop a running timer. Then we use the starting stream and the stopping stream together to create a simple stopwatch. const Observable = Rx.Obser

[RxJS] Basic DOM Rendering with Subscribe

While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscriptions for you, this lesson shows how you can still do basic subscribe blocks and manually update the DOM on your own. const Observable = Rx.Observabl

[Javascript + rxjs] Simple drag and drop with Observables

Armed with the map and concatAll functions, we can create fairly complex interactions in a simple way. We will use Observable to create a simple drag and drop example with basic DOM elements. <!DOCTYPE html> <html> <head lang="en"

rxjs简单入门

rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Observable Sequence),然后订阅序列中那些Observable对象的变化,一旦变化,就会执行事先安排好的各种转换和操作 rxjs适用于异步场景,即前端交互中接口请求.浏览器事件以及自定义事件.通过使用rxjs带给我们前所未有的开发体验. 统一异步编程的规范,不管是Promise.ajax还是

[RxJS] RefCount: automatically starting and stopping an execution

With the connect() method on a ConnectableObservable, the programmer is responsible for avoiding leaked executions of shared RxJS Observables. This lesson will teach you about refCount(), a handy operator that creates an automatically connected Obser

[Recompose] Stream Props to React Children with RxJS

You can decouple the parent stream Component from the mapped React Component by using props.children instead. This process involves mapping the stream to React’s cloneElement with the children then passing the props in manually. We have the code belo