[RxJS] RefCount: automatically starting and stopping an execution
With the connect() method on a ConnectableObservable, the programmer is responsible for avoiding leaked executions of shared RxJS Observables. This lesson will teach you about refCount(), a handy operator that creates an automatically connected Observable, to avoid manually using connect().
After multicast(new Rx.Subject()), we call refCount(), so it will help us to manage the connections, so we don't need to worry about the memory leak.
var shared = Rx.Observable.interval()
.do(x => console.log('source ' + x))
.multicast(new Rx.Subject())
.refCount(); 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); // start 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); // 1 => 2
}, ); setTimeout(function () {
subA.unsubscribe(); // 2 => 1
console.log('unsubscribed A');
}, ); setTimeout(function () {
subB.unsubscribe(); // 1 => 0 (stop)
console.log('unsubscribed B');
}, ); /*
"source 0"
"A next 0"
"source 1"
"A next 1"
"source 2"
"A next 2"
"B next 2"
"source 3"
"A next 3"
"B next 3"
"source 4"
"A next 4"
"B next 4"
"unsubscribed A"
"source 5"
"B next 5"
"source 6"
"B next 6"
"unsubscribed B"
*/
[RxJS] RefCount: automatically starting and stopping an execution的更多相关文章
- 【译】UI设计基础(UI Design Basics)--启动与停止(Starting and Stopping)(五)
2.4 启动与停止(Starting and Stopping) 2.4.1 立即启动(Start Instantly) 通常来讲,用户不会花超过两分钟的时候去评价一个新的应用.在这段有限的时间里 ...
- Starting and Stopping Oracle Fusion Middleware
指定用户名密码启动管理服务器 You can start and stop Oracle WebLogic Server Administration Servers using the WLST c ...
- AFNetworking 与 UIKit+AFNetworking 详解
资料来源 : http://github.ibireme.com/github/list/ios GitHub : 链接地址 简介 : A delightful iOS and OS X networ ...
- 转:AFNetworking 与 UIKit+AFNetworking 详解
资料来源 : http://github.ibireme.com/github/list/ios GitHub : 链接地址 简介 : A delightful iOS and OS X networ ...
- AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking
我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...
- P6 EPPM Installation and Configuration Guide 16 R1 April 2016
P6 EPPM Installation and Configuration Guide 16 R1 April 2016 Contents About Installing and ...
- 【php】使用phpdbg来调试php程序
PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境 可以在PHP5.4和之上版本中使用.在PHP5.6和之上版本将内部集成 功能 单步调试 灵活的下断点 ...
- storm环境搭建(前言)—— 翻译 Setting Up a Development Environment
Setting Up a Development Environment 搭建storm开发环境所需步骤: Download a Storm release , unpack it, and put ...
- Android模拟器使用教程
Using the Emulator In this document Overview Android Virtual Devices and the Emulator Starting and S ...
随机推荐
- ActionListener三种实现
/** * Simple1.java - 处理事件的第一种方法 * 在这个例子中,利用一个ActionListener来监听事件源产生的事件 * 用一些if语句来决定是哪个事件源 */ import ...
- 5个jvm命令
本文是Neward & Associates的总裁Ted Neward为developerworks独家撰稿“你不知道5个……”系列中的一篇,JVM是多数开发人员视为理所当然的Java功能和性 ...
- 【Django】信号调度
Django中提供了"信号调度",用于在框架执行操作时解耦. 通俗来讲,就是在某些动作发生时,信号允许特定的发送者去提醒一些接受者. * Django内置信号:** Model s ...
- postgresql 不同数据库不同模式下的数据迁移
编写不容易,转载请注明出处谢谢, 数据迁移 因为之前爬虫的时候,一部分数据并没有上传到服务器,在本地.本来用的就是postgresql,也没用多久,数据迁移的时候,也遇到了很多问题,第一次使pg_du ...
- POJ——T 1961 Period
http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 18542 ...
- c、c++ 结构体的嵌套
c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...
- js刷新页面有哪几种方法
js刷新页面有哪几种方法 一.总结 一句话总结:location属性的reload方法即可:document.location.reload() 1.页面刷新有哪常见的8种方法? 1,history. ...
- 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)
1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...
- Monkey脚本编写
脚本优势:简单.快捷.不需要借助任何工具,可以做简单的性能测试 脚本缺点:只能简单实现坐标.按键等基本操作,无逻辑性 脚本格式:
- 多线程中的"断点"续传《notify()和wait()》
眼下在做一个项目.关于软件管理与下载的,预计项目提交日期定在6月9号.项目做了有20天了,可是在一个功能上卡住了.在这个项目中有一个功能----APK的下载须要实现. 相信大家都玩过非常多关于下载AP ...