Skip to content

Commit

Permalink
perf: use better suited operators for REST requests (#1491)
Browse files Browse the repository at this point in the history
* replace some concatMap operator with mergeMap
* replace some concatMap operators with switchMap within effects
  • Loading branch information
Eisie96 committed Aug 25, 2023
1 parent 1c0bf6e commit e54f611
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/app/core/store/content/page-tree/page-tree.effects.ts
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, map } from 'rxjs/operators';
import { map, mergeMap } from 'rxjs/operators';

import { CMSService } from 'ish-core/services/cms/cms.service';
import { mapErrorToAction, mapToPayload } from 'ish-core/utils/operators';
Expand All @@ -15,7 +15,7 @@ export class PageTreeEffects {
this.actions$.pipe(
ofType(loadContentPageTree),
mapToPayload(),
concatMap(({ rootId, depth }) =>
mergeMap(({ rootId, depth }) =>
this.cmsService.getContentPageTree(rootId, depth).pipe(
map(pagetree => loadContentPageTreeSuccess({ pagetree })),
mapErrorToAction(loadContentPageTreeFail)
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/content/parameters/parameters.effects.ts
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, mergeMap } from 'rxjs/operators';
import { mergeMap } from 'rxjs/operators';

import { Product } from 'ish-core/models/product/product.model';
import { ProductsService } from 'ish-core/services/products/products.service';
Expand All @@ -21,7 +21,7 @@ export class ParametersEffects {
this.actions$.pipe(
ofType(loadParametersProductListFilter),
mapToPayload(),
concatMap(({ id, searchParameter, amount }) =>
mergeMap(({ id, searchParameter, amount }) =>
this.productsService.getFilteredProducts(searchParameter, amount).pipe(
mergeMap(({ products }) => [
...products.map((product: Product) => loadProductSuccess({ product })),
Expand Down
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, map } from 'rxjs/operators';
import { map, mergeMap } from 'rxjs/operators';

import { CMSService } from 'ish-core/services/cms/cms.service';
import { mapErrorToAction, mapToPayload } from 'ish-core/utils/operators';
Expand All @@ -19,7 +19,7 @@ export class ViewcontextsEffects {
this.actions$.pipe(
ofType(loadViewContextEntrypoint),
mapToPayload(),
concatMap(({ viewContextId, callParameters }) =>
mergeMap(({ viewContextId, callParameters }) =>
this.cmsService.getViewContextContent(viewContextId, callParameters).pipe(
map(({ entrypoint, pagelets }) =>
loadViewContextEntrypointSuccess({ entrypoint, pagelets, viewContextId, callParameters })
Expand Down
Expand Up @@ -48,7 +48,7 @@ export class ServerConfigEffects {
loadServerConfig$ = createEffect(() =>
this.actions$.pipe(
ofType(loadServerConfig),
concatMap(() =>
switchMap(() =>
this.configService.getServerConfiguration().pipe(
map(config => loadServerConfigSuccess({ config })),
mapErrorToAction(loadServerConfigFail)
Expand All @@ -64,7 +64,7 @@ export class ServerConfigEffects {
switchMap(() => this.store.pipe(select(isExtraConfigurationLoaded))),
whenFalsy(),
delayUntil(this.actions$.pipe(ofType(personalizationStatusDetermined))),
concatMap(() =>
switchMap(() =>
this.configService.getExtraConfiguration().pipe(
map(extra => loadExtraConfigSuccess({ extra })),
mapErrorToAction(loadExtraConfigFail)
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/store/customer/basket/basket-items.effects.ts
Expand Up @@ -105,7 +105,7 @@ export class BasketItemsEffects {
this.actions$.pipe(
ofType(addItemsToBasket),
mapToPayload(),
concatMap(payload => [...payload.items.map(item => loadProduct({ sku: item.sku }))])
mergeMap(payload => [...payload.items.map(item => loadProduct({ sku: item.sku }))])
)
);

Expand Down
Expand Up @@ -42,7 +42,7 @@ export class BasketPaymentEffects {
loadBasketEligiblePaymentMethods$ = createEffect(() =>
this.actions$.pipe(
ofType(loadBasketEligiblePaymentMethods),
concatMap(() =>
switchMap(() =>
this.paymentService.getBasketEligiblePaymentMethods().pipe(
map(result => loadBasketEligiblePaymentMethodsSuccess({ paymentMethods: result })),
mapErrorToAction(loadBasketEligiblePaymentMethodsFail)
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/store/customer/basket/basket.effects.ts
Expand Up @@ -116,7 +116,7 @@ export class BasketEffects {
this.actions$.pipe(
ofType(loadBasketByAPIToken),
mapToPayloadProperty('apiToken'),
concatMap(apiToken =>
switchMap(apiToken =>
this.basketService.getBasketByToken(apiToken).pipe(
map(basket => loadBasketSuccess({ basket })),
mapErrorToAction(loadBasketByAPITokenFail)
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/store/customer/orders/orders.effects.ts
Expand Up @@ -122,7 +122,7 @@ export class OrdersEffects {
loadOrders$ = createEffect(() =>
this.actions$.pipe(
ofType(loadOrders),
concatMap(() =>
switchMap(() =>
this.orderService.getOrders().pipe(
map(orders => loadOrdersSuccess({ orders })),
mapErrorToAction(loadOrdersFail)
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/general/countries/countries.effects.ts
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, withLatestFrom } from 'rxjs/operators';
import { filter, map, switchMap, withLatestFrom } from 'rxjs/operators';

import { CountryService } from 'ish-core/services/country/country.service';
import { mapErrorToAction } from 'ish-core/utils/operators';
Expand All @@ -18,7 +18,7 @@ export class CountriesEffects {
ofType(loadCountries),
withLatestFrom(this.store.pipe(select(getAllCountries))),
filter(([, countries]) => !countries.length),
concatMap(() =>
switchMap(() =>
this.countryService.getCountries().pipe(
map(countries => loadCountriesSuccess({ countries })),
mapErrorToAction(loadCountriesFail)
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/general/regions/regions.effects.ts
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, withLatestFrom } from 'rxjs/operators';
import { filter, map, mergeMap, withLatestFrom } from 'rxjs/operators';

import { CountryService } from 'ish-core/services/country/country.service';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
Expand All @@ -19,7 +19,7 @@ export class RegionsEffects {
mapToPayloadProperty('countryCode'),
withLatestFrom(this.store.pipe(select(getAllRegions))),
filter(([countryCode, allRegions]) => !allRegions.some(r => r.countryCode === countryCode)),
concatMap(([countryCode]) =>
mergeMap(([countryCode]) =>
this.countryService.getRegionsByCountry(countryCode).pipe(
map(regions => loadRegionsSuccess({ regions })),
mapErrorToAction(loadRegionsFail)
Expand Down
5 changes: 2 additions & 3 deletions src/app/core/store/shopping/products/products.effects.ts
Expand Up @@ -117,7 +117,7 @@ export class ProductsEffects {
map(payload => ({ ...payload, page: payload.page ? payload.page : 1 })),
withLatestFrom(this.store.pipe(select(getProductListingItemsPerPage('category')))),
map(([payload, pageSize]) => ({ ...payload, amount: pageSize, offset: (payload.page - 1) * pageSize })),
concatMap(({ categoryId, amount, sorting, offset, page }) =>
mergeMap(({ categoryId, amount, sorting, offset, page }) =>
this.productsService.getCategoryProducts(categoryId, amount, sorting, offset).pipe(
concatMap(({ total, products, sortableAttributes }) => [
...products.map(product => loadProductSuccess({ product })),
Expand Down Expand Up @@ -152,8 +152,7 @@ export class ProductsEffects {
map(payload => ({ ...payload, page: payload.page ? payload.page : 1 })),
withLatestFrom(this.store.pipe(select(getProductListingItemsPerPage('master')))),
map(([payload, pageSize]) => ({ ...payload, amount: pageSize, offset: (payload.page - 1) * pageSize })),

concatMap(({ masterSKU, amount, sorting, offset, page }) =>
mergeMap(({ masterSKU, amount, sorting, offset, page }) =>
this.productsService.getProductsForMaster(masterSKU, amount, sorting, offset).pipe(
concatMap(({ total, products, sortableAttributes }) => [
...products.map(product => loadProductSuccess({ product })),
Expand Down
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { concatMap, map } from 'rxjs/operators';
import { concatMap, map, switchMap } from 'rxjs/operators';

import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';

Expand All @@ -25,7 +25,7 @@ export class ContactEffects {
loadSubjects$ = createEffect(() =>
this.actions$.pipe(
ofType(loadContact),
concatMap(() =>
switchMap(() =>
this.contactService.getContactSubjects().pipe(
map(subjects => loadContactSuccess({ subjects })),
mapErrorToAction(loadContactFail)
Expand Down
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction } from '@ngrx/router-store';
import { concatMap, filter, map } from 'rxjs/operators';
import { filter, map, switchMap } from 'rxjs/operators';

import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';

Expand All @@ -25,7 +25,7 @@ export class PunchoutTypesEffects {
loadPunchoutTypes$ = createEffect(() =>
this.actions$.pipe(
ofType(loadPunchoutTypes),
concatMap(() =>
switchMap(() =>
this.punchoutService.getPunchoutTypes().pipe(
map(types => loadPunchoutTypesSuccess({ types })),
mapErrorToAction(loadPunchoutTypesFail)
Expand Down

0 comments on commit e54f611

Please sign in to comment.