In my service, I am making http requests to a backend that is beyond my control in order to retrieve marketing page content. There are instances when I need to load multiple pieces of marketing content simultaneously. I have implemented an effect that calls this service.
@Effect()
marketingContent$ = this.actions$
.ofType(LOAD_MARKETING_CONTENT)
.switchMap(({ payload }) => this.marketingService.getContent(payload)
.map(content => Action.LoadMarketingContentComplete(content))
)
Everything was running smoothly until I realized that if I trigger the loading of more than one piece of marketing content at the same time, the .switchMap
cancels the previous request.
store.dispatch(Action.LoadMarketingContent('A'));
store.dispatch(Action.LoadMarketingContent('B'));
// Only 'B' gets loaded as 'A' is canceled before completion
I tried using .mergeMap
instead of .switchMap
, but then duplicate requests were not being canceled.
Another approach could be creating separate actions for each piece of marketing content, but that would involve setting up new actions and effects for each piece.
Is there a way to modify .switchMap
so that it only cancels requests for the same content (when the payload
matches), or perhaps another method to execute simultaneous requests for different content while canceling duplicates within the same stream?