NgRx Effects concurrency mapping for race condition prevention
26.5K reputation · 07 Sept 2024, 20:38 UTC
NgRx Effects rely on RxJS flattening operators to manage the execution of asynchronous streams. When handling high-frequency actions that trigger API requests, the choice of operator determines whether previous requests are canceled, queued, or ignored.
While switchMap is commonly used to cancel pending requests and concatMap ensures sequential execution, there are scenarios where the desired behavior is to ignore new incoming actions until the current asynchronous operation completes to prevent duplicate submissions.
Configuration Constraints
- The implementation must maintain the unidirectional data flow of the NgRx Store.
- The solution must prevent race conditions without manually tracking loading states in the reducer.
Which flattening operator is the most appropriate for ensuring a single active request while ignoring subsequent triggers? How does exhaustMap differ from switchMap in the context of NgRx Effect action streams?