[RxJS] ReplaySubject with buffer
2024-09-08 17:14:08
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"
*/
最新文章
- WebConfig 自定义节点configSections配置信息
- winrt组件库(包括翻书组件)
- 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单
- for 穷举、迭代 while循环
- OC 实例变量(Instance Var)和成员变量(member var)区别
- VMware vSphere Client的简单使用教程
- SQL Server数据库同步SQL
- ASI进行POST网络请求
- 使用jcifs.smb.SmbFile读取Windows上共享目录的文件
- List泛型集合常用方法
- 第一章(认识jQuery)
- 腾讯织云:DevOps 流水线应用平台践行之路
- Struts2学习---拦截器+struts的工作流程+struts声明式异常处理
- springMVC源码分析--HandlerInterceptor拦截器(一)
- JVM平台上的响应式流(Reactive Streams)规范
- Hexo server报错TypeError: Cannot read property &#39;utcOffset&#39; of null解决方法
- 【Debug】逻辑分析仪数据错乱,看波形为信号耦合导致数据错乱,实际上为逻辑分析仪地线没接上!
- 学号20155308 2016-2017-2 《Java程序设计》第7周学习总结
- react-native-echarts
- Node.js在windows平台的安装
热门文章
- 10.axis实现webservices分布式通信
- 9.Java通过axis调用WebService
- C/C++(结构体)
- offSet和client和scroll
- 使用spring-boot 国际化配置所碰到的乱码问题
- 【2017 Multi-University Training Contest - Team 5】Rikka with Graph
- BZOJ——T 1355: [Baltic2009]Radio Transmission
- CISP/CISA 每日一题 10
- [Node.js] Serve Static Files with Express
- 使用IPV6