[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来处理异步请求,事件调用等 ...
随机推荐
- java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver 错误解决
1.今天在搭建SpringBoot整合 pageHelper的时候报错如下 1.1 引入依赖如下: <!-- 分页插件 --> <dependency> <groupId ...
- GitHub Action一键部署配置,值得拥有
最近由于自己的个人应用增加,每次都需要在服务器手动发布,觉得特别麻烦,所以想通过代码控制自动发布,直接选择了GitHub Action. GitHub Action持续集成服务,目前已经免费开放使用, ...
- springboot读取系统级环境变量,和读写系统属性以及unittest来获取环境变量的方法
环境变量的读取以及系统属性的设置 环境变量只能读取,不能修改,系统属性可以修改 系统变量的读取方式: System.getEnv() 系统属性有多重读取和修改方式: 其修改方式为: 读取系统属性: @ ...
- Java Swing 资料(转载学习)
Swing图像界面简介:https://blog.csdn.net/xietansheng/article/details/72814531 Swing实用经验总结篇:https://blog.csd ...
- flutter apk 打包
https://blog.csdn.net/weixin_33738578/article/details/87998565 http://www.cnblogs.com/sangwl/p/10400 ...
- Unity VS2017 调试外部DLL
之前写的C++ DLL VS2012 都可以附加进程的方式调试Unity中的调用 这次用了一个C# DLL VS2017 在Unity 2018上无法附加进程的方式调试 经过一番折腾, 主要是两个问题 ...
- python day4 元组/字典/集合类知识点补充
目录 python day4 元组/字典/集合类知识点补充 1. 元组tuple知识点补充 2. 字典dict的知识点补充 3. 基本数据类型set 4. 三元运算,又叫三目运算 5. 深复制浅复制 ...
- Vscode配置springboot开发环境变量
先安装必要的插件 然后在左下角setting 打开setting 配置setting.json文件 ,主要是配置了用户设置 这里面主要配置jdk环境和maven,建议下载vscode推荐的openjd ...
- cscope安装
安装 # apt-get install cscope .vimrc中添加 if has("cscope") set csprg=/usr/bin/cscope set csto= ...
- 【Java】锁机制
参考 https://blog.csdn.net/varyall/article/details/79698145 <深入理解Java虚拟机> 锁状态:无锁.偏向锁.轻量级锁.重量级锁(具 ...