日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

SAP Spartacus 读取 Cart 的原理分析

發(fā)布時(shí)間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SAP Spartacus 读取 Cart 的原理分析 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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));}}

首先執(zhí)行 ofType,這本質(zhì)是一個(gè) filter 操作:

數(shù)組的 some 方法:檢查數(shù)組元素是否滿足 predicate 函數(shù)指定的條件

然后執(zhí)行 map 操作,返回一個(gè) OperatorFunction,作為 pipe 的輸入條件之一:

觸發(fā)點(diǎn):

quantity 的值來(lái)自 activeCartService 維護(hù)的 active cart 的 deliveryItemsQuantity 字段。

執(zhí)行 Async pipe:

async pipe 的 transform 方法會(huì)調(diào)用 subscribe 方法:

createSubscription 最終會(huì)調(diào)用:

getActive 返回:

activeCart$ 的值來(lái)自 activeCartLoading$ 和 activeCartValue$ 兩部分。

activeCartLoading$ 負(fù)責(zé)加載 cart,見(jiàn)代碼第 139 行。

調(diào)用的是 ActiveCartService 的 loadCart 方法:

給 store 發(fā)送一個(gè) action。

LoadCart 擴(kuò)展自 EntityLoadAction,除了 payload 之外,定義了額外的字段:

比如 meta:

如果想打印出加載成功的購(gòu)物車信息:

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();

結(jié)果:

那么,這個(gè)加載成功的 Cart 數(shù)據(jù),是如何通過(guò) action 實(shí)例 subscribe 之后被讀取出來(lái)的呢?

顯然,單步調(diào)試第73行代碼,并不會(huì)看到我們想了解的明細(xì)。因?yàn)?subscribe 只是觸發(fā) cart 的加載,而后者是一個(gè)異步過(guò)程。

F8之后斷點(diǎn)再次觸發(fā)時(shí),cart 數(shù)據(jù)已經(jīng)出現(xiàn)在 payload 里了。但是我們不知道是誰(shuí)從哪里通過(guò)什么樣的方式進(jìn)行的回調(diào)。

在 subscriber 的實(shí)現(xiàn)里,能看到當(dāng)前已經(jīng) ready 的 state 值:

ngrx-store.js 在這里將 state 片段的 carts 傳入 map 回調(diào)函數(shù)里:

更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:

總結(jié)

以上是生活随笔為你收集整理的SAP Spartacus 读取 Cart 的原理分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。