SAP 电商云 Spartacus UI 的 checkout 场景中的串行请求设计分析
Current Checkout Design
When we toggle delivery method via radio input, once clicked, there’re three sequential HTTP request sent to backend:
當(dāng)我們點(diǎn)擊 shipping method 時(shí), 會(huì)有三個(gè)串行的 HTTP 請(qǐng)求發(fā)送往后臺(tái)。
這個(gè)串行請(qǐng)求的行為,從 Chrome 開(kāi)發(fā)者工具時(shí)序圖里清晰可見(jiàn)。
Request1: HTTP PUT to set delivery mode
url: https://20.83.184.244:9002/occ/v2/electronics-spa/users/current/carts/00002735/deliverymode?deliveryModeId=standard-gross&lang=en&curr=USD
這個(gè) HTTP put 請(qǐng)求用于修改訂單的交貨模式。
this request is triggered in delivery-mode.component.ts, method changeMode.
模式修改的觸發(fā),從 service 類(lèi)的 setDeliveryMode 開(kāi)始。
The call will be delegated to service class below:
Request2: HTTP get to load [Multi Cart] Multi Cart Data
https://20.83.184.244:9002/occ/v2/electronics-spa/users/current/carts/00002735?fields=DEFAULT,potentialProductPromotions,appliedProductPromotions,potentialOrderPromotions,appliedOrderPromotions,entries(totalPrice(formattedValue),product(images(FULL),stock(FULL)),basePrice(formattedValue,value),updateable),totalPrice(formattedValue),totalItems,totalPriceWithTax(formattedValue),totalDiscounts(value,formattedValue),subTotal(formattedValue),deliveryItemsQuantity,deliveryCost(formattedValue),totalTax(formattedValue,%20value),pickupItemsQuantity,net,appliedVouchers,productDiscounts(formattedValue),user,saveTime,name,description&lang=en&curr=USD
This HTTP GET request is triggered in code: checkout.effect.ts, after previous HTTP PUT request is finished successfully:
當(dāng) HTTP PUT 執(zhí)行完畢后,重新加載 Cart:
Request 3: HTTP get to load [Checkout] Checkout Details
https://20.83.184.244:9002/occ/v2/electronics-spa/users/current/carts/00002735?fields=deliveryAddress(FULL),deliveryMode,paymentInfo(FULL)&lang=en&curr=USD
trigged in checkout-details.service.ts, line 59.
When 2nd HTTP request is finished, cart is stable again, so trigger loadCheckoutDetails method.
Cart 成功加載(stable)之后,加載 checkout Detail 數(shù)據(jù):
當(dāng)前設(shè)計(jì)的缺陷
The problem of current design
Let’s have a look at checkout-delivery.service.ts.
When a user toggles the delivery mode radio input, it DOES NOT mean the HTTP GET request will be sent to backend automatically.
The semantic meaning of line 244 below is: HTTP PUT request could NOT be fired, unless both prerequisites are fulfilled:
通過(guò) Actions 執(zhí)行 setDeliveryMode 的行為是受保護(hù)的,僅當(dāng) Cart stable 并且 isLoading 為 false 時(shí)才執(zhí)行:
The happy path:
15:42:31 355 user toggles with standard delivery mode
15:42:31 360 since cart is stable, checkout loading is false, so fire HTTP PUT request
15:43:31 361 HTTP PUT request is fired
In happy path, HTTP PUT request is fired almost immediately after user clicks radio input.
在 3G 慢速網(wǎng)絡(luò)下測(cè)試會(huì)出現(xiàn)問(wèn)題。
Test under slow 3G network, we have trouble now.
復(fù)現(xiàn)場(chǎng)景
Expected behavior: radio input is disabled again.
Current behavior: radio input is NOT disabled.
The following is time sequence after standard delivery input is clicked again ( test step 4 described above )
Totally 5 seconds passed after user clicks “standard delivery”, unfortunately.
Currently, we disable radio input by deliveryModeSetInProcoess$. However, its value could only be changed when there’s a HTTP PUT request sent to
backend.
deliveryModeSetInProcoess$ 的值只有 HTTP put 操作發(fā)送之后,才有機(jī)會(huì)被修改。
deliveryModeSetInProcess$ = this.checkoutDeliveryService.getSetDeliveryModeProcess().pipe(map((state) => state.loading));而 HTTP put 有兩個(gè)外部依賴(lài):
As explained above, HTTP PUT request sending has two external dependencies:
This means it’s not safe to lock UI by simply using deliveryModeSetInProcess$.
下圖紅線(xiàn)以上的三個(gè)請(qǐng)求,代表點(diǎn)擊 Standard Delivery 之后的 HTTP 請(qǐng)求。
In general, the 1~3 HTTP requests above the red line in picture below represent the requests sent for Standard Delivery radio input click.
下圖紅線(xiàn)以下的三個(gè)請(qǐng)求,代表點(diǎn)擊 Standard Delivery 之后的 HTTP 請(qǐng)求。
the 1~3 HTTP requests below the red line for Premium Delivery radio input click.
If user clicks Premimu Delivery radio input after timestamp t1, that is, after 3rd HTTP request for Standard Delivery is finished, we are safe.
如果用戶(hù)在 t1 時(shí)間戳之后點(diǎn)擊 Premium Delivery,這個(gè)時(shí)候是安全的。
Below is a happy path: t2 > t1
Bad path:
T0: HTTP PUT request has finished. Radio input is enabled again.
If user clicks radio input between (t0, t1), user can toggle between radio input multiple times, however no HTTP put request would be sent( cart should be stable, checkout loading should be false )
Based on Jerry’s observation: such HTTP put request will be queued and sent automatically until cartStable = true and checkout loading = false again.
Any user clicks after t1 will lead to concurrent HTTP PUT request sent to backend.
反之:在t1之前切換 input ,此時(shí) input radio 不能被鎖住,但是點(diǎn)擊并不會(huì)直接觸發(fā) HTTP PUT 操作,而是被加到隊(duì)列里。然后等 t1 到達(dá)時(shí),這些請(qǐng)求像脫韁的頁(yè)碼一樣以并發(fā)的方式發(fā)送出去了。
Time sequence of Jerry’s testing
Test preparation: Premium Delivery is selected. Let’s say wait 20 seconds until all three HTTP request for it ( 1 PUT and 2 GET ) have finished.
Test sequence:
Explain on the console.log:
Part1: radio input is disabled. HTTP put request is sent.
Part2: radio input is enabled again.
Part3: 16:57:02 HTTP GET request for “standard mode” is fired.
16:57:03 user clicks “premium delivery”. No HTTP PUT request will be sent.
Part4: 16:57:04 “standard delivery” is selected. No HTTP put request is sent.
Part5: Only once CartStable becomes true and isLoading becomes false, the HTTP put requests are sent now.
并發(fā)的 HTTP put 請(qǐng)求本身并不會(huì)出錯(cuò),但是會(huì)導(dǎo)致隨后的 HTTP GET 操作返回錯(cuò)誤。
Concurrent HTTP PUT will cause errors in backend API execution:
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的SAP 电商云 Spartacus UI 的 checkout 场景中的串行请求设计分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 云打码实现验证码识别功能_打验证码赚钱真
- 下一篇: SAP 产品线中写法很接近,容易混淆的几