Problem: I need to get response. At first will be called:
this.store.dispatch(new UpdateRequest(data));
then, if success, response will be in :
this.actions.pipe(ofType(ActionTypes.UPDATE_SUCCESS))
or if error:
this.actions.pipe(ofType(ActionTypes.UPDATE_ERROR))
before ngrx it was easy:
this.service.update(data).subscribe((data) => this.update(data)));
with ngrx : A) without take(1):
this.store.dispatch(new UpdateRequest(data));
const success$ = this.actions.pipe(ofType(ActionTypes.UPDATE_SUCCESS))
.pipe(
takeUntil(this.destroy$),
)
.subscribe(() => {
this.update();
success$.unsubscribe();
error.unsubscribe();
});
const error$ = this.actions.pipe(ofType(ActionTypes.UPDATE_ERROR))
.pipe(
takeUntil(this.destroy$),
)
.subscribe(() => {
this.error();
success$.unsubscribe();
error.unsubscribe();
});
B) with take(1): this.store.dispatch(new UpdateRequest(data));
const success$ = this.actions.pipe(ofType(ActionTypes.UPDATE_SUCCESS))
.pipe(
take(1),
takeUntil(this.destroy$),
)
.subscribe(() => {
this.update();
error.unsubscribe();
});
const error$ = this.actions.pipe(ofType(ActionTypes.UPDATE_ERROR))
.pipe(
take(1),
takeUntil(this.destroy$),
)
.subscribe(() => {
this.error();
success$.unsubscribe();
});
And in ngOnDestroy:
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
But in ngrx take(1) is forbidden for action : https://github.com/cartant/eslint-plugin-rxjs/blob/main/docs/rules/no-unsafe-first.md
So, maybe you know better way of subscribing only first value in actions.