[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 是单播的(每个订 ...
随机推荐
- 窗体是不出现在Alt+Tab中(窗体不出现在任务管理器中的应用程序列中)
窗体是不出现在Alt+Tab中和不出现在任务管理器中的应用程序中 重写 CreateParams即可: public class MyForm : Form{ protected override C ...
- 【Codeforces Round #428 (Div. 2) A】Arya and Bran
[Link]: [Description] [Solution] 傻逼题 [NumberOf WA] [Reviw] [Code] #include <bits/stdc++.h> usi ...
- bitset也挺好用的
http://www.cplusplus.com/reference/bitset/bitset/bitset/ std::bitset<16> foo; std::bitset<4 ...
- vim 窗口分割命令
如何在一个窗口下面同时打开两个以及以上的文件,有横向跟纵向两种方式 一.如果在终端中开没有打开vim,可以: 横向分割显示: $ vim -o filename1 filename2 纵向分割显示: ...
- 微信小程序实现一个简单的表格
wxml <view class="table"> <view class="tr bg-w"> <view class=&quo ...
- ZOJ 题目3587 Marlon's String(KMP)
Marlon's String Time Limit: 2 Seconds Memory Limit: 65536 KB Long long ago, there was a coder n ...
- python 命令行參数解析
本文是从我还有一个博客转载过来的,欢迎大家点击进去看一下,帮我添加点人气^_^ ImPyy 选择模块 依据python參考手冊的提示,optparse 已经废弃,应使用 argparse 教程 概念 ...
- Android时间戳与字符串相互转换
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...
- 非极大值抑制(non-maximum suppression)的理解与实现
非极大抑制(Non-Maximum Suppression) Non-Maximum Suppression for Object Detection in Python RCNN 和微软提出的 SP ...
- ios系统和某些移动端background-attachment:fixed不兼容性
固定背景不动:background-attachment:fixed; ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了, ...