I am new to angular + ngrx and have a question about proper control flow at scale. Suppose I have a Cars
component that can can load a list of cars and can create a new car. Once those effects execute, they'll dispatch SUCCESS
and FAILURE
actions. In my ngOnInit
, I am subscribing to each of those success/failure actions to perform appropriate - something like:
ngOnInit() {
this.actions$
.pipe(
ofType<fromCarsActions.GetCarsSuccessAction>(fromCarsActions.CarsActionTypes.GET_CARS_SUCCESS),
takeUntil(this.unsubscribe$)
)
.subscribe(results => {
// do something if load all cars SUCCEEDS
});
this.actions$
.pipe(
ofType<fromCarsActions.GetCarsFailureAction>(fromCarsActions.CarsActionTypes.GET_CARS_FAILURE),
takeUntil(this.unsubscribe$)
)
.subscribe(results => {
// do something if load all cars FAILS
});
this.actions$
.pipe(
ofType<fromCarsActions.AddCarSuccessAction>(fromCarsActions.CarsActionTypes.ADD_CAR_SUCCESS),
takeUntil(this.unsubscribe$)
)
.subscribe(results => {
// do something if add car SUCCEEDS
});
this.actions$
.pipe(
ofType<fromCarsActions.AddCarsFailureAction>(fromCarsActions.CarsActionTypes.ADD_CAR_FAILURE),
takeUntil(this.unsubscribe$)
)
.subscribe(results => {
// do something if add car FAILS
});
}
Subscribing to all these SUCCESS/FAILURE actions is fine if there's only a few, but can easily get out of hand very quickly for more complex components.
Another option I could do is dispatch the appropriate action in the effect itself, but that would not make the effect very reusable.
So my question is what's the best practice for a component that uses NGRX to communicate with the backend to easily manage subscribing to / listening to multiple different effect success/failures?
I understand this is a bit of an abstract question so please let me know if I can clarify anything.