[RxJS] Subject asObservable() method
You can create your own state store, not using any state management libraray.
You might have seen the partten: People create a Subject, then use abObservable() method.
export class State {
private prevState: INEO[];
private neoStoreSubject: BehaviorSubject<INEO[]>;
neoStore$: Observable<INEO[]>;
protected constructor() {
this.neoStoreSubject = new BehaviorSubject([]);
this.neoStore$ = this.neoStoreSubject.asObservable();
}
...
}
Main reason for that is, we want to keep:
this.neoStoreSubject = new BehaviorSubject([]);
as private, we don't want any component can call .next() method to update store. The job for updating store should only happen in 'State' class.
// State class
setNeoStore(neoList: INEO[]) {
this.setPrevState();
this.neoStoreSubject.next(neoList);
this.dismissError();
}
For component, we can subscribe this.neoStore$. it can only receive the data, but not update the store directly.
// From Component biggerFasterNeo$ = this.data.neoStore$.pipe(
filter(neoList => !!neoList === true),
map(neoList => neoList.filter(neo => {
if (neo.estimated_diameter > 0.5 || neo.relative_velocity > ) {
return neo;
}
}))
);
For component
[RxJS] Subject asObservable() method的更多相关文章
- import { Subject } from 'rxjs/Subject';
shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...
- RxJS - Subject(转)
Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...
- [RxJS] Subject basic
A Subject is a type that implements both Observer and Observable types. As an Observer, it can subsc ...
- [RxJS] Subject: an Observable and Observer hybrid
This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...
- [RxJS] Add debug method to Observable in TypeScript
Observable.prototype.debug = function(message: any) { return this.do( (next) => { if(!environment ...
- rxjs——subject和Observable的区别
原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...
- RxJS之Subject主题 ( Angular环境 )
一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...
- [RxJS] Reusable multicasting with Subject factories
The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...
- 【Rxjs】 - 解析四种主题Subject
原文地址: https://segmentfault.com/a/1190000012669794 引言 开发ngx(angular 2+)应用时,基本上到处都会用到rxjs来处理异步请求,事件调用等 ...
随机推荐
- MySQL必知必会3
创建和操纵表 创建表 输入 CREATE TABLE customers ( cust_id int NOT NULL AUTO_INCREMENT, cust_name char(50) NOT N ...
- 下载安装GO,编辑器GOLand和在GOLand上运行项目的几种方式
下载安装GO 下载GO地址:https://golang.google.cn/dl/,一直下一步下一步的操作. 安装完成之后,会有一个GOPATH(此路径是创建go项目的时候会自动在该文件夹下创建), ...
- Eclipse Block Selection(块选择)快捷键 Alt + Shift + A
说实话,我暂时还没用过这个快捷键.但是不代表以后我也不会用它. Eclipse 有个地方可以专门查看这些小技巧. Help → Tip of the Day 进入下面这个窗口: 将 Unread on ...
- 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697
package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...
- 5. RDD编程进阶
5.1 累加器 累加器用来对信息进行聚合,通常在向Spark传递函数时,比如使用map()函数或者用filter()传条件时,可以使用驱动器程序中定义的变量,但是集群中运行的每个任务都会得到这些变量的 ...
- 使用PHP开发HR系统(6)
本节讲述如何连接Postgre数据库并查询与显示数据. ==================================================================== ...
- 使用PHP开发HR系统(4)
本节我们讲述如何引入页面框架来建立漂亮的主页. ============================================================================ ...
- Docker3-Dockerfile创建镜像的方法(推荐docker file这种方法)
一.镜像制作的方法 1.本地导入导出镜像 请参考:Docker 架构原理及简单使用 导出:docker save nginx >/tmp/nginx.tar.gz 导入:docker load ...
- android使用http3
http3的github地址: https://github.com/cloudflare/quiche
- centos 7 安装nginx并启动(笔记)
参考 https://www.cnblogs.com/liujuncm5/p/6713784.html Nginx 是 C语言 开发 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译 ...