[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(1000).take(6)
  .do(x => console.log('source ' + x))
  .map(x => Math.random());

var delayedResult = result.delay(500);
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(1000).take(6)
  .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(1000).take(6)
  .do(x => console.log('source ' + x))
  .map(x => Math.random())
  .multicast(subjectFactory, function sandbox(shared) {
    var delayedShare = shared.delay(500);
    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.

 

posted @   Zhentiw  阅读(475)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-10-26 [Angular 2] Passing data to components with @Input
2015-10-26 [Angular 2] Template property syntax
2015-10-26 [Angular 2] Adding a data model
2015-10-26 [Angular 2] Using ng-model for two-way binding
2015-10-26 [Angular 2] ngFor
点击右上角即可分享
微信分享提示