[RxJS] ReplaySubject with buffer
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"
*/
[RxJS] ReplaySubject with buffer的更多相关文章
- [RxJS] ReplaySubject
A ReplaySubject caches its values and re-emits them to any Observer that subscrubes late to it. Unli ...
- [RxJS] Transformation operator: buffer, bufferCount, bufferTime
This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...
- RxJS库
介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...
- RxJS核心概念之Subjet在angular2+上的应用
Subject,在RxJS中是一类特殊的Observable(可观察对象),它可像多个Observer(观察者)推送值.每一个Subject也可以作为Observer(观察者) Subject同样也是 ...
- 使用 Rx 中预定义的 Subject
看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...
- Hystrix工作流
Hystrix工作流程图: 流程图详解 1. 构造HystrixCommand对象或者HystrixObservableCommand对象 构造HystrixCommand 或HystrixObser ...
- Angular全局数据管理与同步更新
自定义实现angular中数据的状态管理,如有不妥请指正 一.先介绍一下rxjs中subject: Import {subject}from’rxjs’ Subject 数据的订阅与分发,结合报刊的发 ...
- hystrix源码之AbstractCommand
AbstractCommand HystrixCommand和HystrixObservableCommand的父类.每个command对应一个HystrixCommandKey.HystrixThr ...
- [RxJS] AsyncSubject and ReplaySubject - Learn the Differences
We can use Subject as Observable and Observer: // Subject should be only used to emit value for priv ...
随机推荐
- CString与 char *之间的转换
http://www.cnblogs.com/watsonlong/archive/2011/04/15/2017086.html
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- linux系统下的/proc目录介绍
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- C# MQTT 服务端客户端通讯
关于MQTT 在这里我就不做过多的介绍了 , 超时空连接点我 MQTT示例 注: 该示例演示统一使用WPF, 简单MVVM模式演示, 复制代码需注意引用 NuGet包 GalaSoft MQTT服务 ...
- PatentTips - Improving security in a virtual machine host
BACKGROUND Computer viruses are a common problem for computer users. One typical mode of attack is t ...
- PatentTips - Transparent unification of virtual machines
BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...
- 洛谷——P1307 数字反转
https://www.luogu.org/problem/show?pid=1307#sub 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原 ...
- BZOJ2882
传送门:BZOJ2882(权限题) 最小表示法的模板. 传送门:周神论文 代码上的小细节见下. #include <cstdio> #include <cstdlib> #in ...
- ViewPager 入门一
使用ViewPager能够得到不同view的切换效果 例如以下图,实现了四个view间的相互滑动 一.新建项目,引入ViewPager控件 ViewPager.它是google SDk中自带的一个附加 ...
- Android 用Socket实现PC和手机的文件传输
PC服务器端代码: /* * PC与<a href="http://lib.csdn.net/base/android" class='replace_word' title ...