博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] RefCount: automatically starting and stopping an execution
阅读量:5264 次
发布时间:2019-06-14

本文共 1525 字,大约阅读时间需要 5 分钟。

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); // startvar 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"*/

 

转载于:https://www.cnblogs.com/Answer1215/p/5988917.html

你可能感兴趣的文章
Java线程面试题
查看>>
Flask三剑客
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
Java大数——a^b + b^a
查看>>
简单的数据库操作
查看>>
帧的最小长度 CSMA/CD
查看>>
树状数组及其他特别简单的扩展
查看>>
普通求素数和线性筛素数
查看>>
PHP截取中英文混合字符
查看>>
【洛谷P1816 忠诚】线段树
查看>>
电子眼抓拍大解密
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
卷积中的参数
查看>>
51nod1076 (边双连通)
查看>>
ViewPager的onPageChangeListener里面的一些方法参数:
查看>>
Linux pipe函数
查看>>