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 nexterror, 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.

最新文章

  1. Spark踩坑记——数据库(Hbase+Mysql)
  2. ,net core mvc 文件上传
  3. 如何向git账号上提交代码
  4. WebAPI使用多个xml文件生成帮助文档(转)
  5. Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台
  6. C++ 顶层 const
  7. windows下exfat无法写入修复
  8. 改善C#公共程序类库质量的10种方法和工具
  9. SVN中服务器地址变更
  10. 标签传播算法(Label Propagation Algorithm, LPA)初探
  11. 前端面试题整理—jQuery篇
  12. 使用Docker方式运行Mysql(MariaDB)
  13. Django学习---快速搭建搜索引擎(haystack + whoosh + jieba)
  14. Elaticsearch 集群
  15. 【Java】 剑指offer(40) 最小的k个数
  16. B - Build The Electric System 求强连通的最小和//lxm
  17. oracle中number对应java数据类型
  18. shell脚本通过expect脚本实现自动输入密码
  19. 【javascript】—— JS判断浏览器类型、操作系统
  20. nginx源码编译以及源码编译过程中遇到的问题

热门文章

  1. ES6第三节:变量的解构赋值
  2. init进程
  3. 【Codeforces Round #426 (Div. 2) B】The Festive Evening
  4. 14.NPM 常用命令
  5. 洛谷 P1287 盒子与球
  6. SQL-android uri的使用(转载)
  7. 如何从mysql数据库中取到随机的记录
  8. kindle paperwhite 简单笔记按名称分类
  9. poj 1191 棋盘切割 (压缩dp+记忆化搜索)
  10. AbstractQueuedSynchronizer的介绍和原理分析