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. CentOS6.5下的Nagios安装配置详解(图文)

    最近因为,科研需要,接触上了Nagios,这里,我将安装笔记做个详解.为自己后续需要和博友们学习! VMware workstation 11 的下载 VMWare Workstation 11的安装 ...

  2. Snapshot Standby

    INTRODUCTION Snapshot standby database是ORACLE 11g的新特性.允许Physical standby短时间的使用read write模式. Snapshot ...

  3. 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model

    上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...

  4. 获取图书信息api

    https://www.zhihu.com/question/20306982 http://code.juhe.cn/docs/1109 https://developers.douban.com/ ...

  5. Android获取当前连接的wifi名称

    首先AndroidMainfest.xml文件里加入权限: <uses-permission android:name="android.permission.ACCESS_NETWO ...

  6. 怎样在nat方式的虚拟机下做ssh连接

    很多人在本机做測试都是用桥接的方式让虚拟机上网. 假设ip地址紧张或者根本就不同意我们拥有一个局域网的ip.这时候便能够使用NAT方式+putty来远程操作. 第一步,打开设备-Network-更改网 ...

  7. Leetcode47: Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 推断一个链表是不是回文的,一个比較简单的办法是把链表每一个结点的值存在vect ...

  8. startActivityForResult()的用法

    举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Act ...

  9. java创建节点和单向链表

    package datastructure; public class Node { private Object data; private Node next; public Node() { t ...

  10. 2.Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)-

    转自:https://blog.csdn.net/a214919447/article/details/55260411 SOAP(Simple Object Access Protocol,简单对象 ...