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.

最新文章

  1. 基于react实现无限分级菜单
  2. [转] 评 WOW技能天赋设计
  3. 初识JAVA之OOP
  4. cloudera learning2:HDFS
  5. <context-param>与<init-param>
  6. iOS开发-动态和静态FrameWork
  7. tarjan算法--求无向图的割点和桥
  8. ASP.NET MVC 入门系列教程
  9. PROTEL99生成GERBER的操作说明
  10. python绝技 — 用Scapy测试无线网卡的嗅探功能
  11. POJ1837--二维背包
  12. hash加密
  13. SpringData使用与整合
  14. sql server 作业收缩数据库
  15. Maven_1 安装配置
  16. 基于C#的PISDK研究(代码)
  17. 实现一套山寨springMVC
  18. JS正则表达式方法
  19. 偏最小二乘回归(PLSR)- 2 标准算法(NIPALS)
  20. 项目开发-->高级功能汇总

热门文章

  1. LA 2531 The K-league 最大流
  2. spring webSocket The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
  3. C# 返回分页查询的总页数
  4. WPF MVVM架构 EF、WCF、IOC 设计示例经典
  5. hdu 1233 还是畅通project (克鲁斯卡尔裸题)
  6. 电商系统Broadleaf文档翻译(六) - 主要实体main entities
  7. linux/unix 基本概念的认识(sha-bang 、PPA)
  8. 洛谷P2147 [SDOI2008]Cave 洞穴勘测
  9. 用硬件卡克隆Linux集群
  10. (转)修改 ubuntu 默认启动项