A BehaviorSubject can remember the latest value emitted, but what if we wanted Observer B to see all the previous values emitted in the past? We can't do that with BehaviorSubject, but there is ReplaySubject, which allows us to do that. This lessons teaches you everything you need to know about ReplaySubjects.

It is not good to cache all the value by using ReplaySubject, so we need to add cache logic for this.

The first parameter to the constructor of ReplaySubject takes a number that represents how many values we want to buffer:

var subject = new Rx.ReplaySubject(); // Buffer size of 2
subject.next();
subject.next();
subject.next();
subject.subscribe(function(n) {
console.log('Received value:', n);
}); /*
"Received value:"
3
"Received value:"
2
*/

The second parameter takes a number that represents the time in miliseconds during which we want to buffer values:

var subject = new Rx.ReplaySubject(null, ); // Buffer size of 200ms
setTimeout(function() { subject.next(); }, ); //350(subscribe time) - 200 (buffer time) > 100 --> not replay
setTimeout(function() { subject.next(); }, ); //350 - 200 < 200 --> replay
setTimeout(function() { subject.next(); }, ); //350 - 200 < 300 --> replay
setTimeout(function() {
subject.subscribe(function(n) {
console.log('Received value:', n);
});
subject.onNext();
}, ); /*
"Received value:"
2
"Received value:"
3
"Received value:"
4
*/
var subject = new Rx.ReplaySubject();
// new Rx.BehaviorSubject(0); var observerA = {
next: function (x) { console.log('A next ' + x); },
error: function (err) { console.log('A error ' + err); },
complete: function () { console.log('A done'); },
}; subject.subscribe(observerA);
console.log('observerA subscribed'); var observerB = {
next: function (x) { console.log('B next ' + x); },
error: function (err) { console.log('B error ' + err); },
complete: function () { console.log('B done'); },
}; setTimeout(() => subject.next(), );
setTimeout(() => subject.next(), );
setTimeout(() => subject.next(), );
setTimeout(() => subject.complete(), ); /*
----1---2---3--|
..1...2...3...
1,2,3|
*/ setTimeout(function () {
subject.subscribe(observerB);
console.log('observerB subscribed');
}, ); /*
"observerA subscribed"
"A next 1"
"A next 2"
"A next 3"
"A done"
"B next 1"
"B next 2"
"B next 3"
"B done"
"observerB subscribed"
*/

最新文章

  1. WebConfig 自定义节点configSections配置信息
  2. winrt组件库(包括翻书组件)
  3. 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单
  4. for 穷举、迭代 while循环
  5. OC 实例变量(Instance Var)和成员变量(member var)区别
  6. VMware vSphere Client的简单使用教程
  7. SQL Server数据库同步SQL
  8. ASI进行POST网络请求
  9. 使用jcifs.smb.SmbFile读取Windows上共享目录的文件
  10. List泛型集合常用方法
  11. 第一章(认识jQuery)
  12. 腾讯织云:DevOps 流水线应用平台践行之路
  13. Struts2学习---拦截器+struts的工作流程+struts声明式异常处理
  14. springMVC源码分析--HandlerInterceptor拦截器(一)
  15. JVM平台上的响应式流(Reactive Streams)规范
  16. Hexo server报错TypeError: Cannot read property &#39;utcOffset&#39; of null解决方法
  17. 【Debug】逻辑分析仪数据错乱,看波形为信号耦合导致数据错乱,实际上为逻辑分析仪地线没接上!
  18. 学号20155308 2016-2017-2 《Java程序设计》第7周学习总结
  19. react-native-echarts
  20. Node.js在windows平台的安装

热门文章

  1. 10.axis实现webservices分布式通信
  2. 9.Java通过axis调用WebService
  3. C/C++(结构体)
  4. offSet和client和scroll
  5. 使用spring-boot 国际化配置所碰到的乱码问题
  6. 【2017 Multi-University Training Contest - Team 5】Rikka with Graph
  7. BZOJ——T 1355: [Baltic2009]Radio Transmission
  8. CISP/CISA 每日一题 10
  9. [Node.js] Serve Static Files with Express
  10. 使用IPV6