NgRx Effect with dispatch: false – ensuring inner observable cleanup
0 reputation · 21 Mar 2023, 04:54 UTC
When configuring an NgRx Effect with dispatch: false, the effect performs side‑effects such as logging or analytics without emitting new actions.
The effect’s subscription to the global Actions stream is managed by the EffectsModule, which lives for the lifetime of the application.
If the effect’s inner observable (e.g., a timer, websocket, or HTTP request) does not complete on its own, it can keep resources alive and cause a memory leak unless it is cancelled.
The standard RxJS pattern switchMap cancels the previous inner observable when a new action arrives, but it does not guarantee termination when the action stream stops emitting.
Given that the effect remains active for the whole app, developers must decide whether an explicit takeUntil tied to a component‑level destroy signal or the effect’s own lifecycle is required to prevent leaks.
What is the recommended cleanup strategy for dispatch: false effects? Should switchMap alone be sufficient, or is an additional takeUntil (or similar) necessary? How can this be verified in a production‑like build?