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. Linux下安装zip解压功能

    liunx服务器上默认没有安装zip命令,所以使用时需安装:apt-get install zip 或  yum install zip linux安装unzip命令:apt-get install ...

  2. 5.Maven之(五)Maven仓库

    转自:https://blog.csdn.net/oonmyway1234/article/details/82315777 本地仓库 Maven一个很突出的功能就是jar包管理,一旦工程需要依赖哪些 ...

  3. js的style和getArribute("属性名")

    getAttribute()是HTML DOM的一个方法,用以获取HTML元素的属性(如id,name,type以及其他自定义属性). style是HTML DOM的一个关于样式的对象,style对象 ...

  4. wget---从指定的URL下载文件

    wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...

  5. [置顶] Docker学习总结(3)——Docker实战之入门以及Dockerfile(三)

    应用镜像 csphere/wordpress:4.2 # cd docker-training/wordpress/ # ls -a . license.txt wp-config-sample.ph ...

  6. Oracle中NVL、NVL2、DECODE函数的用法

    DECODE函数的用法:   DECODE(value,if1,then1,if2,then2,if3,then3,......,else),表示如果value的值等于if1时,DECODE函数的结果 ...

  7. 洛谷 P1827 美国血统 American Heritage

    P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树 ...

  8. python RESTful API框架:Eve 高速入门

    Eve是一款Python的REST API框架.用于公布高可定制的.全功能的RESTful的Web服务.帮你轻松创建和部署API,本文翻译自Eve官方站点: http://python-eve.org ...

  9. Windows简单入门-送给第一次使用电脑的朋友

    序言 写本篇文章前.不得不说我已经好久没有写博客了,快接近3个月了 吧,本来去年说參加今年的博客之星的,结果这都立即结束了:不得不说对自己有些嘲讽. 本篇文章是纯小白文章.之所以写这个是由于前段时间妹 ...

  10. Vue 消息无缝滚动

    vue实现消息向上无缝滚动效果 <ul class="new-list" :class="{anim:animate}" @mouseenter=&quo ...