Skip to content

Commit

Permalink
perf: prevent unnecessary actions (#1580)
Browse files Browse the repository at this point in the history
* perf: prevent firing price action twice
* perf: prevent firing empty add to cart
* perf: reduce firing product links request
* perf: reduce number of variations actions
  • Loading branch information
dhhyi authored Jan 29, 2024
1 parent 29173be commit 093f1ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/app/core/facades/shopping.facade.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, combineLatest, identity } from 'rxjs';
import { debounce, filter, map, pairwise, startWith, switchMap, tap } from 'rxjs/operators';
import { debounce, distinctUntilChanged, filter, map, pairwise, startWith, switchMap, tap } from 'rxjs/operators';

import { PRICE_UPDATE } from 'ish-core/configurations/injection-keys';
import { PriceItemHelper } from 'ish-core/models/price-item/price-item.helper';
Expand Down Expand Up @@ -120,6 +120,7 @@ export class ShoppingFacade {
select(getProductPrice(plainSKU)),
// reset state when updates are forced
this.priceUpdate === 'always' || fresh ? startWith(undefined) : identity,
distinctUntilChanged(),
tap(prices => {
if (!prices) {
this.store.dispatch(loadProductPrices({ skus: [plainSKU] }));
Expand Down Expand Up @@ -162,10 +163,16 @@ export class ShoppingFacade {
productLinks$(sku: string | Observable<string>) {
return toObservable(sku).pipe(
whenTruthy(),
tap(plainSKU => {
this.store.dispatch(loadProductLinks({ sku: plainSKU }));
}),
switchMap(plainSKU => this.store.pipe(select(getProductLinks(plainSKU))))
switchMap(plainSKU =>
this.store.pipe(
select(getProductLinks(plainSKU)),
tap(links => {
if (!links) {
this.store.dispatch(loadProductLinks({ sku: plainSKU }));
}
})
)
)
);
}

Expand Down
1 change: 1 addition & 0 deletions src/app/core/store/customer/basket/basket-items.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class BasketItemsEffects {
// accumulate all actions
window(this.actions$.pipe(ofType(addProductToBasket), debounceTime(500))),
mergeMap(window$ => window$.pipe(toArray())),
filter(items => items.length > 0),
map(items => addItemsToBasket({ items }))
)
);
Expand Down
8 changes: 7 additions & 1 deletion src/app/core/store/shopping/products/products.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,13 @@ export class ProductsEffects {
: undefined
),
whenTruthy(),
map(sku => loadProductVariationsIfNotLoaded({ sku }))
groupBy(identity),
mergeMap(group$ =>
group$.pipe(
throttleTime(10),
map(sku => loadProductVariationsIfNotLoaded({ sku }))
)
)
),
{ dispatch: true }
);
Expand Down

0 comments on commit 093f1ec

Please sign in to comment.