The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusable if the shared execution happens to complete or emit an error. In this lesson we will see how to use a simple Subject factory function in order to create a new Subject, one for each shared execution, whenever connect() is called.

var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(new Rx.Subject())
.refCount();

The code above, after subject emit 0,1,2, three values, then it completes. It means if you want to subscribe the subject again, it won't emit anything because it is completed.

If you want to reuse the 'shared' subject even after subject complete, you need to use subject factories, which simply just a function return new Subject():

function subjectFactory() {
return new Rx.Subject();
} var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(subjectFactory)
.refCount();

So now even you resubscribe after subject complete, it will emit you new value.

function subjectFactory() {
return new Rx.Subject();
} var shared = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.multicast(subjectFactory)
.refCount(); // subject: --0--1--2--3--4--5|
// A
// subject2: --0--1--2--3--4--5| 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 subA = shared.subscribe(observerA); // 0 => 1
console.log('subscribed A'); 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 = shared.subscribe(observerB);
console.log('subscribed B');
}, ); setTimeout(function () {
subA.unsubscribe();
console.log('unsubscribed A');
}, ); setTimeout(function () {
subB.unsubscribe();
console.log('unsubscribed B');
}, ); setTimeout(function () {
subA = shared.subscribe(observerA); // 0 => 1 (connect)
console.log('subscribed A');
}, );
/**
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"subscribed B"
"source 2"
"A next 2"
"B next 2"
"A done"
"B done"
"unsubscribed A"
"unsubscribed B"
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"source 2"
"A next 2"
"A done" */

[RxJS] Reusable multicasting with Subject factories的更多相关文章

  1. RxJS学习笔记之Subject

    本文为原创文章,转载请标明出处 目录 Subject BehaviorSubject ReplaySubject AsyncSubject 1. Subject 总的来说,Subject 既是能够将值 ...

  2. RxJS - Subject(转)

    Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...

  3. 【Rxjs】 - 解析四种主题Subject

    原文地址: https://segmentfault.com/a/1190000012669794 引言 开发ngx(angular 2+)应用时,基本上到处都会用到rxjs来处理异步请求,事件调用等 ...

  4. 使用 Rx 中预定义的 Subject

    看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...

  5. RxJS 6有哪些新变化?

    我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6.  rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...

  6. Angular 2 技能图谱skill-map

    # Angular 2 技能图谱 ## 模块 ### 自定义模块 - 根模块 - 特性模块 - 共享模块 - 核心模块 ### 内置模块 - ApplicationModule 模块 - Common ...

  7. RXJS Observable的冷,热和Subject

    一.Observable的冷和热 Observable 热:直播.所有的观察者,无论进来的早还是晚,看到的是同样内容的同样进度,订阅的时候得到的都是最新时刻发送的值. Observable 冷:点播. ...

  8. RxJS之Subject主题 ( Angular环境 )

    一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...

  9. import { Subject } from 'rxjs/Subject';

    shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...

随机推荐

  1. python版 百度签到

    经常玩贴吧,刚学python ,所以自己弄了一个python版的签到程序.自己的东西总是最好的. 登陆模块参考的http://www.crifan.com/emulate_login_website_ ...

  2. 数字签名算法--3.ECDSA

    package Imooc; import java.security.KeyFactory; import java.security.KeyPair; import java.security.K ...

  3. Qt样式表——选择器详解(父子关系)

    在上一节中,小豆君给大家介绍了样式表的基本概念和大致用法.今天我们来详细了解下样式表中选择器的用法. 所谓选择器,就是指定你所设置的样式对哪个或哪些控件起作用. 到目前为止,Qt样式表支持CSS2中定 ...

  4. Impala是什么?

    Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统虽然也提供了SQL语义,但由于Hive底层 ...

  5. windows新建或者重命名文件及文件夹必须手动刷新才能显示出来

    平台:win8.1 问题:windows新建或者重命名文件及文件夹必须手动刷新才能显示出来 解决方法: 注册表中HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ ...

  6. sql server还原数据库代码

    RESTORE DATABASE ExaminationsystemFROM DISK = 'C:\Users\admin\Desktop\20140324.bak'with replace,MOVE ...

  7. CISP/CISA 每日一题 16

    CISA 每日一题(答) 作业调度软件的优点: 1.作业信息仅需建立一次,减少错误发生概率: 2.可定义作业间的依赖关系,当某一项作业失败时,依赖于该作业的后续作业就不会被执行: 3.所有成功或失败的 ...

  8. [TS] Implement a doubly linked list in TypeScript

    In a doubly linked list each node in the list stores the contents of the node and a pointer or refer ...

  9. hdu4605Magic Ball Game 树状数组

    //给一棵树.树的每个节点的子节点个数是0或2 //对于每个节点都有一个权值w[i] //一个权值为x的球在每个节点的情况有 //x=w[i] 这个球在该点不向下掉 //x<w[i] 这个球往左 ...

  10. Cts框架解析(1)-windows下cts配置

    环境搭建 下载 cts工具的下载地址:http://source.android.com/compatibility/downloads.html windows选择Android4.4 R3 Com ...