[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(1000)
  .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
}, 2000);

setTimeout(function () {
  subA.unsubscribe(); // 2 => 1
  console.log(‘unsubscribed A‘);
}, 5000);

setTimeout(function () {
  subB.unsubscribe(); // 1 => 0 (stop)
  console.log(‘unsubscribed B‘);
}, 7000);

/*
"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"
*/
时间: 2024-10-15 16:10:14

[RxJS] RefCount: automatically starting and stopping an execution的相关文章

Starting and Stopping Oracle Fusion Middleware

指定用户名密码启动管理服务器 You can start and stop Oracle WebLogic Server Administration Servers using the WLST command line or a script. When you start or stop the Administration Server, you also start or stop the processes running in the Administration Server,

转:AFNetworking 与 UIKit+AFNetworking 详解

资料来源 : http://github.ibireme.com/github/list/ios GitHub : 链接地址 简介 : A delightful iOS and OS X networking framework. 推荐参考 : http://afnetworking.com http://www.aiuxian.com/article/p-1537192.html 链接地址一. 文件目录 链接地址1. AFNetworking 目录内容 链接地址2. UIKit+AFNetwo

ZooKeeper Java Example

A Simple Watch Client Requirements Program Design The Executor Class The DataMonitor Class Complete Source Listings A Simple Watch Client To introduce you to the ZooKeeper Java API, we develop here a very simple watch client. This ZooKeeper client wa

P6 EPPM Installation and Configuration Guide 16 R1 April 2016

P6 EPPM Installation and Configuration Guide 16 R1         April 2016 Contents About Installing and Configuring P6 EPPM ........................................................ 6 Prerequisites for P6 EPPM Configuration ...............................

Andriod Atom x86模拟器启动报错。

用Inter Atom模式的Android模拟器启动报一下错误: Starting emulator for AVD 'new' emulator: ERROR: x86 emulation currently requires hardware acceleration! Please ensure Intel HAXM is properly installed and usable. CPU acceleration status: HAX kernel module is not ins

四)Spring + Quartz

使用Quartz默认配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframewo

How to stop a Thread(怎样停止一个线程)

Abstract How to stop a Thread is a perannual question for Java programmers. Finally with the release of Java V5.0 (or V1.5), which incorporates java.util.concurrent, a definitive answer can be given. The answer is you stop a thread by using interrupt

Video for Linux Two API Specification Revision 2.6.32【转】

转自:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html Video for Linux Two API Specification Revision 2.6.32 Michael H Schimek <[email protected]> Bill Dirks Original author of the V4L2 API and documentation. Hans

OS Watcher

今天客户需要使用Os Watcher,就简单的学习了一下.这里贴出官方手册,方便没有mos账号的同学 OSWatcher now provides an analysis tool oswbba which analyzes the log files produced by OSWatcher. This tool allows OSWatcher to be self-analyzing. This tool also provides a graphing capability to gr