ConnectableObservable has the connect() method to conveniently dictate the start of the shared execution of the source Observable. However, we need a mechanism to dictate the stop of the shared execution, otherwise a leak happens. This lesson will teach you how to do that, and it's all about Subscriptions.

var connectableObservable = Rx.Observable.interval(1000)
.do(x => console.log('source ' + x))
.multicast(new Rx.Subject()); var observerA = {
next: function (x) { console.log('A next ' + x); },
error: function (err) { console.log('A error ' + err); },
complete: function () { console.log('A done'); },
}; var sub = connectableObservable.connect(); // start var subA = connectableObservable.subscribe(observerA); var observerB = {
next: function (x) { console.log('B next ' + x); },
error: function (err) { console.log('B error ' + err); },
complete: function () { console.log('B done'); },
}; var subB;
setTimeout(function () {
subB = connectableObservable.subscribe(observerB);
}, 2000); setTimeout(function () {
sub.unsubscribe(); // stop
console.log('unsubscribed shared execution');
}, 5000);

Just remember that with connect we are manually controlling the start of the shared execution, and then we keep a subscription in order to manually control the stop of the shared execution. All of this is in order to avoid leaks.

[RxJS] Stopping a shared observable execution的更多相关文章

  1. [RxJS] Stopping a Stream with TakeUntil

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

  2. RxJS库

    介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...

  3. Angular快速学习笔记(4) -- Observable与RxJS

    介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...

  4. [RxJS] Subject: an Observable and Observer hybrid

    This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...

  5. RxJS——可观察的对象(Observable)

    可观察的(Observable) 可观察集合(Observables)是多值懒推送集合.它们填补了下面表格的空白: SINGLE MULTIPLE Pull Function Iterator Pus ...

  6. rxjs——subject和Observable的区别

    原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...

  7. [RxJS] Reusable multicasting with Subject factories

    The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...

  8. RxJS v6 学习指南

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

  9. [RxJS] Multicast with a selector argument, as a sandbox

    Let's explore a different use of the multicast() operator in RxJS, where you can provide a selector ...

随机推荐

  1. css中linear-gradient的使用

    注明:此篇文章来自http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-text-shadow.html.

  2. 深入了解"网上邻居"原理

    说到“网上邻居”,相信很多人都很熟悉.但是说起“网上邻居”的工作机制,可能大家就不太清楚了. 要说“网上邻居”的工作机制,不妨联系一下生活中的例子:比如我(A),要拜访一个远方的朋友(B),我要去他的 ...

  3. C/C++(指针数组)

    指针数组 指针数组的本质是数组,数组指针的本质是指针 一个数组中的各个元素都是字符指针,即为字符指针数组,或者指针数组. int arr[] = {1,2,23,45,6};//整形数组 char c ...

  4. node内容

    什么是node? node是一门技术,并不是一门新的语言.是JavaScript运行环境. node在解析JavaScript,依靠的是什么? 依靠的是谷歌浏览器中的v8引擎,所以不需要考虑兼容性,n ...

  5. Funui-overlay 如何添加theme 的 overlay

    昨天更改theme主题的时候,发现所有仓库下的theme都是共用的.也就是说,如果你更改了52平台下的theme,那么你提交了代码以后,82下也会发生相应的更改.但是,昨天修改的theme属性,只在3 ...

  6. php函数按地址传递参数(php引用)

    php函数按地址传递参数(php引用) 一.总结 1.php引用:php引用和c++一样,都是在变量前加&(取地址符号) 2.php函数按地址传递参数:php函数按地址传递参数(php引用)也 ...

  7. Linux 内存管理与系统架构设计

    Linux 提供各种模式(比如,消息队列),但是最著名的是 POSIX 共享内存(shmem,shared memory). Linux provides a variety of schemes ( ...

  8. jmeter响应数据中文乱码问题

    进入jmeter安装文件目录:D:\Program File\apache-jmeter-2.13\apache-jmeter-2.13\bin\ 修改jmeter.properties文件,在最下方 ...

  9. web.xml的配置及加载顺序

    一web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...

  10. word2vec源代码解析之word2vec.c

    word2vec源代码解析之word2vec.c 近期研究了一下google的开源项目word2vector,http://code.google.com/p/word2vec/. 事实上这玩意算是神 ...