So I'm usually having a method that dispatches an action on click:
create() {
this.store.dispatch(new Create(this.form.value));
}
this code triggers the following scenario and dispatches a CreateSuccess or CreateFailed depending of if the request failed or not
@Action(Create)
create({ dispatch }: StateContext<StateInterface>, { entity}: Create) {
return this.http.post('mybackend', entity).pipe(
tap<EntityType>(resp => dispatch(new CreateSuccess(resp))),
catchError(error => dispatch(new CreateFailed(error)))
);
}
Now inside the Component where I called the create()
I'm listening for those two actions.
this.actions$.pipe(
ofActionSuccessful(CreateSuccess, UpdateSuccess),
takeUntil(componentDestroyed(this)) // <--
).subscribe(action => doStuff());
This all works flawlessly, the only thing that bothers me is that every time I use this, I have to add the takeUntil() part so the subscription is cancelled when the component is destroyed.
I know this is probably not a real issue for everyone, but I'd like to know if there is a cleaner way to do this.
I've thought about a custom RxJS operator that could handle this, but maybe there are other options, or (something I haven't found anything about), is there a way that NGXS does the unsubscribing itself?