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();
}, );

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().take(); /*
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的更多相关文章

  1. [RxJS] Use RxJS concatMap to map and concat high order observables

    Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...

  2. [RxJS] Use RxJS mergeMap to map and merge high order observables

    Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...

  3. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  4. 构建流式应用—RxJS详解[转]

    目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observe ...

  5. 【CuteJavaScript】Angular6入门项目(3.编写服务和引入RxJS)

    本文目录 一.项目起步 二.编写路由组件 三.编写页面组件 1.编写单一组件 2.模拟数据 3.编写主从组件 四.编写服务 1.为什么需要服务 2.编写服务 五.引入RxJS 1.关于RxJS 2.引 ...

  6. Observable详解

    Observable详解 rxjs angular2 在介绍 Observable 之前,我们要先了解两个设计模式: Observer Pattern - (观察者模式) Iterator Patte ...

  7. Rx.js实现原理浅析

    前言 上次给大家分享了cycle.js的内容,这个框架核心模块的代码其实只有一百多行,要理解这个看似复杂的框架,其实最核心的是理解它依赖的异步数据流处理框架--rx.js.今天,给大家分享一下rx.j ...

  8. [RxJS] Filtering operators: takeUntil, takeWhile

    take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...

  9. [RxJS] Stopping a Stream with TakeUntil

    Observables often need to be stopped before they are completed. This lesson shows how to use takeUnt ...

随机推荐

  1. java(异常体系及权限修饰符)

    java异常体系 异常的体系: 异常体系: --------| Throwable 所有错误或者异常的父类 --------------| Error(错误) --------------| Exce ...

  2. 今日SGU 5.9

    SGU 297 题意:就是求余数 收获:无 #include<bits/stdc++.h> #define de(x) cout<<#x<<"=" ...

  3. 调用WCF出现的异常

    使用如下代码调用调用远程服务时,                   try                    {                        using (GetSimServ ...

  4. mvc定时执行任务(获取气象台的气象数据,定时新增)

    1.定时任务: gloabl.asax文件Application_Start()方法注册: System.Timers.Timer t = new System.Timers.Timer(theInt ...

  5. vue-cli(脚手架) 打包上线

    http://www.cnblogs.com/xueweijie/p/6971146.html

  6. JavaScript--数据结构与算法之列表

    3.1 列表的抽象数据类型定义 列表:一组有序的数据.每个列表中的数据称为元素.在JavaScript中列表的元素可以是任意的数据类型.列表中保存的元素没有事先的限定,实际使用时的元素数量受到程序内存 ...

  7. 【2017百度之星程序设计大赛 - 复赛】Valley Numer

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6148 [题意] 在这里写题意 [题解] 先把1..N里面的山峰数字个数算出来->x 然后用N减去这 ...

  8. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...

  9. @Import注解

    转自:https://blog.csdn.net/heyutao007/article/details/74994161 @Import注解就是之前xml配置中的import标签,可以用于依赖第三方包 ...

  10. Codeforces_GYM_100741 A

    http://codeforces.com/gym/100741/problem/A A. Queries time limit per test 0.25 seconds memory limit ...