File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
modules/component-store/src Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1
1
export * from './component-store' ;
2
+ export * from './tap-response' ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments