[Angular] State provider solutions
There are many ways to expose data to components, but based on different usecase, different solution has PROS & CONS.
1. ShareReplay(1)
PROS: ShareReplay(1) solves a problem which if you have multi subscribers, it will trigger API requests multi times if you don't have shareReplay(1) in place.
CONS: data is static, not dynamic. If you want to add/remove/update the data, you have to fetch new data again.
2. Subject as a service
We can keep a local copy of data using BehaviorSubject. Everytime, we add/remove/update, will trigger subject update.
PROS: Solve the problem for static data, now it is dynamic and it won't trigger multi API calls.
CONS: It has race condition. If you try to add multi-times. it might happne that first request's response comes after second request's response.
3. NgRx (ComponentStroe)
https://ngrx.io/guide/component-store
ComponentStore vs Global store: https://ngrx.io/guide/component-store/comparison
We need state for some components, it is not necessary add to global store.
PROS: It handles race condition by using effect (you can define your approach by using concatMap, switchMap, mergeMap, exhaustMap)
concatMap: is a good way to solve race condition.
switchMap: only care new request, ignore old one
exhaustMap: only care the current happening one, ignore new ones during the period
Performance it better, NgRx using select() with memorized function.
CONS: Deep knowledge of RxJS and NgRx.
Misc:
tapResponse:
From
to
Refer:
Filling the Gap in State with NgRx ComponentStore | Alex Okrushko | EnterpriseNG 2020
原文地址:https://www.cnblogs.com/Answer1215/p/15371235.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 掌握Rabbitmq几个重要概念,从一条消息说起
- 超赞!墙裂推荐一个 MySQL 自动化运维工具!
- 设计模式-责任链模式
- 问题 linux下执行.sh 文件出现 no such file or directoryile
- 问题:ssm关于mybatis没有找到配置问题(not found)
- 设计模式-享元模式
- 设计模式-代理模式
- win10 x64下从0开始搭建YApi可视化接口管理平台
- dubbo(一)SPI机制与实现路径
- Go 使用三方 Redis 包操作 Redis
- 干货|这可能是最全的高并发、高性能、高可用解决方案总结
- 群晖多人协作开发
- 分享 Druid 连接池参数优化实战
- 使用 Eureka 简单实现服务健康监控日志分析
- Flask 入门系列教程(一)