[RxJS] Stopping a shared observable execution
2024-09-30 01:42:20
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.
最新文章
- 基于react实现无限分级菜单
- [转] 评 WOW技能天赋设计
- 初识JAVA之OOP
- cloudera learning2:HDFS
- <;context-param>;与<;init-param>;
- iOS开发-动态和静态FrameWork
- tarjan算法--求无向图的割点和桥
- ASP.NET MVC 入门系列教程
- PROTEL99生成GERBER的操作说明
- python绝技 — 用Scapy测试无线网卡的嗅探功能
- POJ1837--二维背包
- hash加密
- SpringData使用与整合
- sql server 作业收缩数据库
- Maven_1 安装配置
- 基于C#的PISDK研究(代码)
- 实现一套山寨springMVC
- JS正则表达式方法
- 偏最小二乘回归(PLSR)- 2 标准算法(NIPALS)
- 项目开发-->;高级功能汇总
热门文章
- LA 2531 The K-league 最大流
- spring webSocket The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
- C# 返回分页查询的总页数
- WPF MVVM架构 EF、WCF、IOC 设计示例经典
- hdu 1233 还是畅通project (克鲁斯卡尔裸题)
- 电商系统Broadleaf文档翻译(六) - 主要实体main entities
- linux/unix 基本概念的认识(sha-bang 、PPA)
- 洛谷P2147 [SDOI2008]Cave 洞穴勘测
- 用硬件卡克隆Linux集群
- (转)修改 ubuntu 默认启动项