fix(effects): use switchMap for list-fetch effects to prevent stale data#2369
Open
alubbock wants to merge 2 commits into
Open
fix(effects): use switchMap for list-fetch effects to prevent stale data#2369alubbock wants to merge 2 commits into
alubbock wants to merge 2 commits into
Conversation
Replace mergeMap with switchMap in fetchDatasets$, fetchFacetCounts$, fetchMetadataKeys$ (datasets.effects) and fetchProposalDatasets$ (proposals.effects). With mergeMap, rapidly changing filters or sort order left multiple concurrent requests in flight. Responses arriving out of order could overwrite newer store state with stale results. switchMap cancels the previous in-flight HTTP request when a new action arrives, so only the latest query's response is ever committed to the store. The inner mergeMap calls used to dispatch multiple actions from a single HTTP response are intentionally unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replace
mergeMapwithswitchMapin the four list-fetching NgRx effects that handle user-driven queries (filter changes, sort, pagination).Motivation
With
mergeMap, rapidly changing filters or sort order leaves multiple concurrent HTTP requests in flight simultaneously. Responses can arrive out of order and overwrite newer store state with stale results. For example, a slow first request completing after a faster second one would silently replace the correct data with stale results.switchMapcancels the previous in-flight request when a new action arrives, so only the response to the latest query is ever committed to the store. Angular'sHttpClientsupports cancellation, so the superseded request is also dropped on the network side.Fixes:
fetchDatasets$indatasets.effects.tsfetchFacetCounts$indatasets.effects.tsfetchMetadataKeys$indatasets.effects.tsfetchProposalDatasets$inproposals.effects.tsThe inner
mergeMapcalls that dispatch multiple actions from a single HTTP response are intentionally unchanged.Changes:
datasets.effects.tsandproposals.effects.ts"-a-b"/"--a|"pattern: two rapid actions with a delayed response, asserting only the second response reaches the storeTests included
Summary by Sourcery
Use cancellable NgRx effects for list-fetching actions to avoid stale data when queries change rapidly.
Bug Fixes:
Tests: