[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 类的源码,从源码中去了解它的实现与应用方法. ...
随机推荐
- deep-in-es6(一)
一 迭代器和for-of循环 以前的一些遍历数组: function c(n) { console.log(n); } 方法一: for(let i = 0;i < arr.length;i++ ...
- 【2017 Multi-University Training Contest - Team 4】Counting Divisors
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6069 [Description] 定义d(i)为数字i的因子个数; 求∑rld(ik) 其中l,r ...
- Effective C++ 条款43
学习处理模板化基类里的名称 本节作者编写的意图在我看来能够总结成一句话,就是"怎样定义并使用关于模板类的派生过程,怎样处理派生过程出现的编译不通过问题". 以下我们看一段说明性的代 ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- js34
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- ajax嵌套ajax 可能出现问题 的解决办法
ajax由于他的异步特性 在第一次请求中的循环中嵌套第二个ajax会数据会读不出来 第一种 描述:如果条件许可,把两次请求都放在服务端处理掉一起发回来,这些就在客户端只有一次ajax了 优点:代码放在 ...
- HDU1203 I NEED A OFFER! 【贪心】
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- PatentTips - Use of multiple virtual machine monitors to handle privileged events
BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...
- ajax利用php上传图片
<script type="text/javascript"> window.onload = function(){ document.getElementById( ...
- sass和less,优秀的前端样式预处理器
身为切图界的一员,或者说在前端界打滚了一段日子的你.会慢慢地发现.如今的css编写已经不能满足自己的效率. 假设有更强大的框架,让你的css更灵活和更easy复用和维护,那该多好啊.非常明显,这个早已 ...