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" */

最新文章

  1. [转]中国最大的Webshell后门箱子调查,所有公开大马全军覆没
  2. 编写一个简单的jdbc例子程序
  3. mha安装使用手册
  4. 如何安装Oracle Instant Client
  5. Struts2-S2-032远程命令执行EXP
  6. C# 基础 计算平均值的方法
  7. Jsch
  8. Oracle12c创建新用户提示公共用户名或角色无效
  9. UVA 658 It's not a Bug, it's a Feature!
  10. 关于Intent的七大属性
  11. 用Jpush极光推送实现抓取特定某个用户Log到七牛云服务器
  12. java+selenium3.0 运行时出的问题(system property)!
  13. Python实战之网络编程socket学习笔记及简单练习
  14. UNIX网络编程——Socket粘包问题
  15. 广州.NET微软技术俱乐部微信群各位技术大牛的blog
  16. Istio入门实战与架构原理——使用Docker Compose搭建Service Mesh
  17. conda命令简单使用
  18. selenium操作浏览器
  19. HGOI 20180224 题解
  20. 【编码题篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题

热门文章

  1. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
  2. ActiveMQ学习总结(2)——ActiveMQ入门实例教程
  3. 南阳oj 士兵杀敌(二) 题目116 NYOJ 数据结构
  4. 洛谷P2234 [HNOI2002]营业额统计(01Tire树)
  5. 不用浏览器,直接用代码发送文件给webservices所在服务器 并且可以周期行的发送
  6. golang 函数作为类型
  7. golang filepath.Walk遍历指定目录下的所有文件
  8. 一文了解sun.misc.Unsafe
  9. Angular:了解Typescript
  10. 洛谷——P1073 最优贸易 ([NOIP2009] )