[RxJS] Multicast with a selector argument, as a sandbox
Let's explore a different use of the multicast() operator in RxJS, where you can provide a selector function as a sandbox where the shared Observable is available.
When we have code like below:
var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random()); var delayedResult = result.delay();
var merged = result.merge(delayedResult); merged.subscribe( (x) => console.log(x))
/*
"source 0"
0.5832993222895915
"source 0"
0.031394357976560316
"source 1"
0.27602687148865
"source 1"
0.8762531748833942
"source 2"
0.49254272653868103
"source 2"
0.8024593359949526
...
*/
You can notice that, it runs 'result' block twice each time, it because 'merged' subscribe to 'result' and 'delayedResult' also subscribe to 'result', therefore it log out source twice.
If you only want one subscribe, you can use multicast(), with a second param which is sandbox function.
Normally you will use mulitcast() with refCount():
function subjectFactory() {
return new Rx.Subject();
}
var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random())
.multicast(subjectFactory).refCount();
var sub = result.subscribe(x => console.log(x));
If you pass a second param:
var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random())
.multicast(subjectFactory, function sandbox(shared) {
var delayedShare = shared.delay();
var merged = shared.merge(delayedShare);
return merged;
}); result.subscribe(x => console.log(x));
/*
"source 0"
0.9214861149095479
0.9214861149095479
"source 1"
0.1684919218677523
0.1684919218677523
"source 2"
0.28182876689689795
0.28182876689689795
...
*/
Notice that, is you pass a second param to multicase(), the return value is no longer an connectableObservable. It is just a normal observable. So you cannot call 'refCount()' anymore.
And inside sandbox() function, you need to retrun a observable.
From the results can see, we no longer subscribe the source twice.
The takeaway is you should use a selector function in multicast when you want to create, let's say, a diamond-shaped dependency. Here we have a bifurcation. As you see we have shared, and it's used in two parts, and then we converge those two parts together to return one observable. That's kind of like a diamond shape, where we bifurcate, and then we converge.
That is one case where you almost always want to use a selector function in multicast. If you don't, then usually we use just multicast with a refCount. That's quite common to use.
[RxJS] Multicast with a selector argument, as a sandbox的更多相关文章
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- rxjs的世界
rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...
- [RxJS] Conclusion: when to use Subjects
As a conclusion to this course about RxJS subjects, let's review when and why should you use them. F ...
- RxJS之工具操作符 ( Angular环境 )
一 delay操作符 源Observable延迟指定时间,再开始发射值. import { Component, OnInit } from '@angular/core'; import { of ...
- RxJS之转化操作符 ( Angular环境 )
一 map操作符 类似于大家所熟知的 Array.prototype.map 方法,此操作符将投射函数应用于每个值 并且在输出 Observable 中发出投射后的结果. import { Compo ...
- RxJS之过滤操作符 ( Angular环境 )
一 take操作符 只发出源 Observable 最初发出的的N个值 (N = count). 如果源发出值的数量小于 count 的话,那么它的所有值都将发出.然后它便完成,无论源 Observa ...
- RxJS之组合操作符 ( Angular环境 )
一 merge操作符 把多个 Observables 的值混合到一个 Observable 中 import { Component, OnInit } from '@angular/core'; i ...
- Angular Multiple HTTP Requests with RxJS
原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...
- Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析
上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...
随机推荐
- js简易留言板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- factor---将素数分解为质数
- logrotate---日志分割
logrotate命令用于对系统日志进行轮转.压缩和删除,也可以将日志发送到指定邮箱.使用logrotate指令,可让你轻松管理系统所产生的记录文件.每个记录文件都可被设置成每日,每周或每月处理,也能 ...
- 使用 Bluemix™ Live Sync 高速更新 Bluemix 上执行的应用程序实例
假设您要构建 Node.js 应用程序,那么能够使用 IBM® Bluemix® Live Sync 高速更新 Bluemix 上的应用程序实例,并像在桌面上进行操作一样进行开发,而无需又一次部署.执 ...
- 终结者:借助pinyin4j相关jar包提取汉字的首字母
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
- UVa 11094 - Continents
题目:有一些岛屿在湖中.地图用两种字符表示.当前处在位置是一个岛屿.求除了当前岛屿外的最大岛屿. 分析:图论,floodfill.直接利用dfs求联通部分的面积就可以,然后取出最大. 说明:横线没有边 ...
- Qt虽然自己的源代码里不使用Exception,但也提供了一个QException及其子类QUnhandledException
http://doc.qt.io/qt-5/exceptionsafety.htmlhttp://doc.qt.io/qt-5/qexception.htmlhttp://doc.qt.io/qt-5 ...
- 解决Firefox不信任StartSSL证书问题
从2016年的11月份开始,firefox \ chrome \ apple 等陆续不再信任 StartSSL 的证书,导致一些使用 StartSSL 的证书的网站访问遇到了麻烦, firefo ...
- 18.C语言多线程总结
创建一个线程 _beginthread(myfun, , NULL);//返回值是一个HANDLE hd[i] = CreateThread(NULL, , add, NULL, , NULL);// ...
- 74.sscanf数据扫描
"%[0-9A-Za-z] 读取一个集合,遇到不是数组或者大小写字母跳出 %*[^0-9A-Za-z]读取所有的非数字字母的字符,忽略 示例: ]= "123sadsadasd ...