当前位置:网站首页>SAP Spartacus 读取 Cart 的原理分析
SAP Spartacus 读取 Cart 的原理分析
2022-07-20 14:21:00 【华为云】
App.module.ts 的源代码:
export class AppModule { constructor(private config: DebugConfig, private actions$: Actions, private cartService: ActiveCartService){ // console.log('Jerry config: ', this.config); this.actions$.pipe( ofType(CartActions.LOAD_CART), map((action: CartActions.LoadCart) => action.payload), tap((data) => console.log('Jerry cart: ' , data))).subscribe(); this.cartService.getLoading().subscribe((data) => console.log('Jerry cart loading? ', data)); }}
首先执行 ofType,这本质是一个 filter 操作:
数组的 some 方法:检查数组元素是否满足 predicate 函数指定的条件
然后执行 map 操作,返回一个 OperatorFunction,作为 pipe 的输入条件之一:
触发点:
quantity 的值来自 activeCartService 维护的 active cart 的 deliveryItemsQuantity 字段。
执行 Async pipe:
async pipe 的 transform 方法会调用 subscribe 方法:
createSubscription 最终会调用:
getActive 返回:
activeCart$ 的值来自 activeCartLoading$ 和 activeCartValue$ 两部分。
activeCartLoading$ 负责加载 cart,见代码第 139 行。
调用的是 ActiveCartService 的 loadCart 方法:
给 store 发送一个 action。
LoadCart 扩展自 EntityLoadAction,除了 payload 之外,定义了额外的字段:
比如 meta:
如果想打印出加载成功的购物车信息:
const action2 = this.actions$.pipe( ofType(CartActions.LOAD_CART_SUCCESS), map((action: CartActions.LoadCartSuccess) => action.payload), tap((data) => console.log('Jerry cart SUCCESS: ' , data))); action2.subscribe(); const action3 = this.actions$.pipe( ofType(CartActions.LOAD_CARTS_SUCCESS), map((action: CartActions.LoadCartsSuccess) => action.payload), tap((data) => console.log('Jerry carts SUCCESS: ' , data))); action3.subscribe();
结果:
那么,这个加载成功的 Cart 数据,是如何通过 action 实例 subscribe 之后被读取出来的呢?
显然,单步调试第73行代码,并不会看到我们想了解的明细。因为 subscribe 只是触发 cart 的加载,而后者是一个异步过程。
F8之后断点再次触发时,cart 数据已经出现在 payload 里了。但是我们不知道是谁从哪里通过什么样的方式进行的回调。
在 subscriber 的实现里,能看到当前已经 ready 的 state 值:
ngrx-store.js 在这里将 state 片段的 carts 传入 map 回调函数里:
边栏推荐
- 暑假打工 2 个月,让我明白了 Keepalived 高可用的三种路由方案
- C语言中长度为零的数组详解 (1)【文章结尾有资料】
- C language: file operation: sequential reading and writing, random reading and writing
- 时间的魔术
- 【iCore4 双核心板_ARM】例程二十二:LWIP_UDP实验——以太网数据传输
- (作业)C语言做题:内存函数memmove、memcpy的模拟
- 【性能优化】MySQL常用慢查询分析工具
- harbor安装
- 大咖说·图书分享 | HaaS 物联网设备云端一体开发框架
- 可观测性提升软件工程质量,观测云出席2022 QECon全球软件质量&效能大会
猜你喜欢
Dependency injection
暑假打工 2 个月,让我明白了 Keepalived 高可用的三种路由方案
(作业)C语言做题:结构体对齐数、位段、联合体
EfCore 表关系一对一,使用案例
New Crack:::CAD .NET 14.1.X
The IPO of minshi group was terminated: Qin ronghua, who had planned to raise 6.5 billion yuan, was punished by the court
依赖注入
openGauss内核分析:查询重写
Airbnb的动态kubernetes集群扩缩容
中国石墨上市:市值近12亿港元 实现鹤岗民营企业上市零突破
随机推荐
Taobao / tmall get the description of the return value of Taobao live broadcast classification ID interface API
MATLAB 点云粗糙度计算
C语言:文件操作:顺序读写、随机读写
PyTorch 结构重参数化 RepVGGBlock
如何进行服务器防勒索病毒系统是我们不得不考虑的问题了
关于果子的思考
深入Go Map的使用技巧
安装 scoop和lux (原annie)
mysql修改密码(手把手教你)
Fraud detection using CSP
VB. Net plug-in development case
Matlab point cloud roughness calculation
iOS_计算缓存文件大小和清除缓存
BreederDAO 第一项提案发布:DAO 组织的宪法章程
View disk space usage
查看磁盘空间占用情况
2012.4.13 360笔试总结
一个字符串“0”,竟是B站崩溃的元凶...
windows server2012 r2中搭建PHP
7.18 - 每日一题 - 408