Skip to content

Commit d1873c9

Browse files
feat(component-store): add tapResponse operator (#2763)
* feat(component-store): add handleResponse operator * Rename to mapResponse * rename to tapResponse
1 parent a942ac6 commit d1873c9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

modules/component-store/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './component-store';
2+
export * from './tap-response';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { EMPTY, Observable } from 'rxjs';
2+
3+
import { catchError, tap } from 'rxjs/operators';
4+
5+
/**
6+
* Handles the response in ComponentStore effects in a safe way, without
7+
* additional boilerplate.
8+
* It enforces that the error case is handled and that the effect would still be
9+
* running should an error occur.
10+
*
11+
* Takes an optional third argument for a `complete` callback.
12+
*
13+
* ```typescript
14+
* readonly dismissedAlerts = this.effect<Alert>(alert$ => {
15+
* return alert$.pipe(
16+
* concatMap(
17+
* (alert) => this.alertsService.dismissAlert(alert).pipe(
18+
* tapResponse(
19+
* (dismissedAlert) => this.alertDismissed(dismissedAlert),
20+
* (error) => this.logError(error),
21+
* ))));
22+
* });
23+
* ```
24+
*/
25+
export function tapResponse<T>(
26+
nextFn: (next: T) => void,
27+
errorFn: (error: unknown) => void,
28+
completeFn?: () => void
29+
): (source: Observable<T>) => Observable<T> {
30+
return (source) =>
31+
source.pipe(
32+
tap({
33+
next: nextFn,
34+
error: errorFn,
35+
complete: completeFn,
36+
}),
37+
catchError(() => EMPTY)
38+
);
39+
}

0 commit comments

Comments
 (0)