[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. For certain cases, subjects are absolutely necessary. If we map to random numbers and we wish two or more observers to see the same random numbers, then we must use a subject
here like we used inside the multicast
. This means that if you're doing some side effect and you don't want to perform that side effect for every observer, then you need a subject.
var result = Rx.Observable.interval().take()
.do(x => console.log('source ' + x))
.map(x => Math.random())
.multicast(subjectFactory, function selector(shared) {
var sharedDelayed = shared.delay();
var merged = shared.merge(resultDelayed);
return merged;
});
We also need to decide what to do when an observer arrives late. Do we keep the latest value in memory and resend it? That's why we have ReplaySubject
and BehaviorSubject
. Use them when you need to cache values or you need to represent something as a value over time, not an event stream, like a person's age versus a stream of birthdays.
As a recap, a subject
is the only type of observable that contains a list of attached observers, so subscribing to a subject
is like adding a listener. In contrast, subscribing to a normal observable is like invoking an execution of a sequence of values.
Every subject
is also an observer which makes it possible for you to subscribe
to an observable and using a subject
as the observer. This happens on under the hood every time you use multicast
.
Essentially, we're invoking the values of the observable and we're delivering them on the subject
, and then we can add multiple listeners to the subject
.
Because the subject
is an observer, it has those methods next
, error
, and complete
which means that we can use a subject
like an event emitter. So whenever you need an event emitter that plays well with the rest of RxJS, then you need a subject
.
Just don't abuse this API because, in many cases where people use subjects as event emitters, they could have just used normal observables. It's preferable to use observables because they bring better separation of concerns and also they bring a cleaning up of resources in an automatic way.
As we saw, you need to be careful with the subjects so you don't create a useless execution that no one is observing. For instance, when we use connect
manually, there we have the responsibility over that because we may be creating a leak.
The takeaway is subjects are necessary, but it's easy to do the wrong thing when using them directly. That's why it's better to let them stay under the hood by using multicast
and its variance like publish
, and publishReplay
, and those. Then you get the benefit of subjects without the dangers of them.
[RxJS] Conclusion: when to use Subjects的更多相关文章
- [Angular 2] Handling Click Events with Subjects
While Angular 2 usually uses event handlers to manage events and RxJS typically uses Observable.from ...
- 使用 Rx 中预定义的 Subject
看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...
- Approach for Unsupervised Bug Report Summarization 无监督bug报告汇总方法
AUSUM: approach for unsupervised bug report summarization 1. Abstract 解决的bug被归类以便未来参考 缺点是还是需要手动的去细读很 ...
- [RxJS] Convert RxJS Subjects to Observables
The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they ca ...
- RxJS - Subject(转)
Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...
- RxJS库
介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...
- Angular18 RXJS
1 RX 全称是 Reactive Extensions,它是微软开发并维护的基于 Reactive Programming 范式实现的一套工具库集合:RX结合了观察者模式.迭代器模式.函数式编程来管 ...
- [RxJS] Connection operator: multicast and connect
We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple obse ...
- RxJS——主题(Subject)
主题(Subjects) 什么是主题?RxJS 主题就是一个特性类型的 Observable 对象,它允许值多路广播给观察者(Observers).当一个简单的 Observable 是单播的(每个订 ...
随机推荐
- Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解
一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...
- eclipse- MAT安装及使用
1.安装eclipse mat插件 1)查看当前eclipse版本 进入eclipse目录:右击eclipse图标,看到安装目录/home/zhangshuli/adt-bundle-linux-x8 ...
- thinkphp5 left join
thinkphp5 left join 一.总结 1.作用:left join就是即使不匹配也返回左表中的数据 2.join使用通式:object join ( mixed join [, mixed ...
- 李笑来~执行力WWH
什么是秘密 秘密是指只有极少数人知道的实用信息.这个实用信息可以为知道且懂得运用的人获得收益,这个收益可能包括钱.名声和快感. 什么是执行力 执行力=What + Why + How,即WWH 执行力 ...
- idea添加自动编译
话不多说,idea每次修改文件不自动编译到项目里,这里做一下一些操作 registry快捷键ctrl+shift+alt+/
- Git提交.net项目的小问题
今天早上写了点关于asp.net core授权的东西,输入git add .的时候出现的报错 $ git add .error: open(".vs/DOTNETAuthorization/ ...
- Python操作MySQL数据库完成简易的增删改查功能
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...
- vue2.0实现银行卡类型种类的选择
功能效果:vue2.0实现银行卡类型种类的选择 图片.png 参考代码如下: <template> <div class="app"> <header ...
- iOS 友盟分享
iOS 友盟分享 这个主要是提到怎样通过友盟去自己定义分享的步骤: 一.肯定要去友盟官网下载最新的SDK包,然后将SDK导入到你的project目录里面去. 二.注冊友盟账号.将你的APP加入到你的账 ...
- Qt源码分析之信号和槽机制(QMetaObject是一个内部struct)
Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的说法,简单点说就是如何在一个类的一个 ...