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的更多相关文章

  1. [RxJS] ReplaySubject

    A ReplaySubject caches its values and re-emits them to any Observer that subscrubes late to it. Unli ...

  2. [RxJS] Transformation operator: buffer, bufferCount, bufferTime

    This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...

  3. RxJS库

    介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...

  4. RxJS核心概念之Subjet在angular2+上的应用

    Subject,在RxJS中是一类特殊的Observable(可观察对象),它可像多个Observer(观察者)推送值.每一个Subject也可以作为Observer(观察者) Subject同样也是 ...

  5. 使用 Rx 中预定义的 Subject

    看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...

  6. Hystrix工作流

    Hystrix工作流程图: 流程图详解 1. 构造HystrixCommand对象或者HystrixObservableCommand对象 构造HystrixCommand 或HystrixObser ...

  7. Angular全局数据管理与同步更新

    自定义实现angular中数据的状态管理,如有不妥请指正 一.先介绍一下rxjs中subject: Import {subject}from’rxjs’ Subject 数据的订阅与分发,结合报刊的发 ...

  8. hystrix源码之AbstractCommand

    AbstractCommand HystrixCommand和HystrixObservableCommand的父类.每个command对应一个HystrixCommandKey.HystrixThr ...

  9. [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 ...

随机推荐

  1. js --- for in 和 for of

    前言:for of是ES6新增的循环方法.前面已经说到了 [JavaScript]for.forEach .for in.each循环详解.那for of又是怎么使用的? 一.使用例子 使用例子(一) ...

  2. Spring Cloud Sleuth通过Kafka将链路追踪日志输出到ELK

    1.工程简介 elk-eureka-server作为其他三个项目的服务注册中心 elk-kafka-client调用elk-kafka-server,elk-kafka-server再调用elk-ka ...

  3. Mindjet MindManager 思维导图软件-使用思维导图跟踪调用流程,绘制软件框架

    思维导图.据说是每一个产品经理必备的软件.假设你阅读大型源码.使用思维导图跟踪调用流程,绘制软件框架将会很方便. 特点:没什么好说的.用过的都说好. 软件截图: 下载:http://www.mindm ...

  4. js34

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. 一个发邮件的demo 用golang

    一个比较成熟的第三方包用来发邮件,可以带图片 和附件,项目地址 : github.com/go-gomail/gomail 一个发邮件的demo 用golang 文件目录树: -d:\test\goe ...

  6. BZOJ1576: [Usaco2009 Jan]安全路经Travel(树链剖分)

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  7. 03002_MySQL数据库的安装和配置

    1.MySQL的安装 (1)下载mysql-5.5.49-win32.msi, 链接:MySQL安装包下载 密码:geqh : (2)打开下载的MySQL安装文件mysql-5.5.27-win32. ...

  8. wmi 一些配置(参考)

    http://www.bubuko.com/infodetail-1937463.html

  9. HDU 3232 &amp;&amp; UVA 12230 (简单期望)

    Crossing Rivers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. word2vec源代码解析之word2vec.c

    word2vec源代码解析之word2vec.c 近期研究了一下google的开源项目word2vector,http://code.google.com/p/word2vec/. 事实上这玩意算是神 ...