We can use Subject as Observable and Observer:

// Subject should be only used to emit value for private
// Using subject as an Observer
const subject = new Subject(); subject.next(1);
subject.next(2);
subject.next(3); // Using subject as an Observable
const source$ = subject.asObservable(); source$.subscribe(console.log);

AsyncSubject:

AsyncSubject only subscribe the latest value before subject complete.

const as = new AsyncSubject();

as.next(1);
as.next(2);
as.next(3);
as.complete(); // it is necessary to complete it in order to get last value const source$ = as.asObservable(); source$.subscribe(console.log); //

If we don't call 'as.complete()', we won't get any value in the console.

Different from AsyncSubject, we have ReplySubject:

It doesn't need to call complete() in order to emit value. And it emit all the values from every beginning.

const rs = new ReplaySubject();

rs.next(1);
rs.next(2);
rs.next(3); const source$ = as.asObservable(); source$.subscribe(console.log); // 1,2,3

[RxJS] AsyncSubject and ReplaySubject - Learn the Differences的更多相关文章

  1. [RxJS] AsyncSubject: representing a computation that yields a final value

    This lesson will teach you about AsyncSubject, another type of Subject with some replaying logic ins ...

  2. [RxJS] AsyncSubject

    AsyncSubject emit the last value of a sequence only if the sequence completed. This value is then ca ...

  3. RxJS之AsyncSubject

    AsyncSubject 是另一个 Subject 变体,只有当 Observable 执行完成时(执行 complete()),它才会将执行的最后一个值发送给观察者. import { Compon ...

  4. What are the differences between struct and class in C++?

    Question: This question was already asked in the context of C#/.Net. Now I'd like to learn the diffe ...

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

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

  6. Difference between ref and out parameters

    Original link: http://www.dotnet-tricks.com/Tutorial/csharp/K0Hb060414-Difference-between-ref-and-ou ...

  7. Using FireMonkey Layouts

    FireMonkey has many layout controls to choose from. Come learn the differences and how to use them t ...

  8. OrientDB入门(1)Getting Started

    Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...

  9. 学习笔记:Rick's RoTs -- Rules of Thumb for MySQL

    Table of Contents SELECTs -- do's and don'tsINDEXingENGINE DifferencesOptimizations, and notPARTITIO ...

随机推荐

  1. phpStorm更新后配置svn无法使用

    phpStorm迎来新的更新,结果之前配置的svn竟然无法使用啦,快捷键一类也没有作用.各种查找解决方案,最后找到解决方案.点击File找到Settings,找到Plugins这个部分,这个部分是管理 ...

  2. linux 如何查看进程的执行时间

    ps  -ef|grep wo.php 得到 程序的pid 如 123 ps -p 123 -o etime

  3. 去掉myeclipse的预览窗口

    1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...

  4. Elasticsearch集群状态健康值处于red状态问题分析与解决(图文详解)

      问题详情 我的es集群,开启后,都好久了,一直报red状态??? 问题分析 有两个分片数据好像丢了.   不知道你这数据怎么丢的. 确认下本地到底还有没有,本地要是确认没了,那数据就丢了,删除索引 ...

  5. Elasticsearch之sense插件的安装(图文详解)

    sense插件可以方便的执行rest请求,但是中文输入的体验不是很好. 安装sense只需要在Kibana端安装插件即可,插件会自动安装到kibana的应用菜单中. [hadoop@master ki ...

  6. 查询分析器执行SQL很快但是ado.net很慢:请为你的SQLparameter设置DbType

    我们都知道,参数化查询可以处理SQL注入,以及提高查询的效率,因为参数化查询会使MSSQL缓存查询的计划. 但是会出现一个问题:有的时候参数化查询比直接拼接sql字符串效率低好多,甚至是查询超时. 原 ...

  7. Zabbix 监控磁盘IO

    Zabbix 监控磁盘IO 1.数据获取脚本 #!/bin/bash # resource: http://www.muck.net/19/getting-hard-disk-performance- ...

  8. day03-执行python方式、变量及数据类型简介

    目录 执行Python程序的两种方式 1. 第一种:交互式 2. 第二种:命令式 3. Python执行程序的三个阶段 变量 变量 什么是变量 Python中的变量 变量名的命名规范 内存管理 定义变 ...

  9. 【Flutter学习】基本组件之AppBar顶部导航栏

    一,概述 AppBar 显示在app的顶部.AppBar包含5大部分,如下图: 二,构造函数及参数含义 构造函数 AppBar({ Key key, this.leading, //在标题前面显示的一 ...

  10. 一个带关闭按钮的Div窗口,很漂亮

    <html><head><title>JS+CSS实现带关闭按钮的DIV弹出窗口</title><script> function lock ...