[RxJS] Reusable multicasting with Subject factories

The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusable if the shared execution happens to complete or emit an error. In this lesson we will see how to use a simple Subject factory function in order to create a new Subject, one for each shared execution, whenever connect() is called.

 

var shared = Rx.Observable.interval(1000).take(3)
  .do(x => console.log('source ' + x))
  .multicast(new Rx.Subject())
  .refCount();

The code above, after subject emit 0,1,2, three values, then it completes. It means if you want to subscribe the subject again, it won't emit anything because it is completed. 

 

If you want to reuse the 'shared' subject even after subject complete, you need to use subject factories, which simply just a function return new Subject():

function subjectFactory() {
  return new Rx.Subject(); 
}

var shared = Rx.Observable.interval(1000).take(3)
  .do(x => console.log('source ' + x))
  .multicast(subjectFactory)
  .refCount();

 

So now even you resubscribe after subject complete, it will emit you new value.

复制代码
function subjectFactory() {
  return new Rx.Subject(); 
}

var shared = Rx.Observable.interval(1000).take(3)
  .do(x => console.log('source ' + x))
  .multicast(subjectFactory)
  .refCount();

// subject: --0--1--2--3--4--5|
//                               A
// subject2:                     --0--1--2--3--4--5|

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); // 0 => 1
console.log('subscribed A');

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);
  console.log('subscribed B');
}, 2000);

setTimeout(function () {
  subA.unsubscribe();
  console.log('unsubscribed A');
}, 3000);

setTimeout(function () {
  subB.unsubscribe();
  console.log('unsubscribed B');
}, 5000);

setTimeout(function () {
  subA = shared.subscribe(observerA); // 0 => 1 (connect)
  console.log('subscribed A');
}, 6000);
复制代码

 

复制代码
/**
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"subscribed B"
"source 2"
"A next 2"
"B next 2"
"A done"
"B done"
"unsubscribed A"
"unsubscribed B"
"subscribed A"
"source 0"
"A next 0"
"source 1"
"A next 1"
"source 2"
"A next 2"
"A done"

*/
复制代码

 

posted @   Zhentiw  阅读(318)  评论(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
点击右上角即可分享
微信分享提示