|
1 | 1 | import { EMPTY, Observable } from 'rxjs';
|
| 2 | +import { catchError, finalize, tap } from 'rxjs/operators'; |
2 | 3 |
|
3 |
| -import { catchError, tap } from 'rxjs/operators'; |
| 4 | +type TapResponseObserver<T, E> = { |
| 5 | + next: (value: T) => void; |
| 6 | + error: (error: E) => void; |
| 7 | + complete?: () => void; |
| 8 | + finalize?: () => void; |
| 9 | +}; |
4 | 10 |
|
| 11 | +export function tapResponse<T, E = unknown>( |
| 12 | + observer: TapResponseObserver<T, E> |
| 13 | +): (source$: Observable<T>) => Observable<T>; |
| 14 | +export function tapResponse<T, E = unknown>( |
| 15 | + next: (value: T) => void, |
| 16 | + error: (error: E) => void, |
| 17 | + complete?: () => void |
| 18 | +): (source$: Observable<T>) => Observable<T>; |
5 | 19 | /**
|
6 | 20 | * 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. |
| 21 | + * additional boilerplate. It enforces that the error case is handled and |
| 22 | + * that the effect would still be running should an error occur. |
| 23 | + * |
| 24 | + * Takes optional callbacks for `complete` and `finalize`. |
| 25 | + * |
| 26 | + * @usageNotes |
10 | 27 | *
|
11 |
| - * Takes an optional third argument for a `complete` callback. |
| 28 | + * ```ts |
| 29 | + * readonly dismissAlert = this.effect<Alert>((alert$) => { |
| 30 | + * return alert$.pipe( |
| 31 | + * concatMap( |
| 32 | + * (alert) => this.alertsService.dismissAlert(alert).pipe( |
| 33 | + * tapResponse( |
| 34 | + * (dismissedAlert) => this.alertDismissed(dismissedAlert), |
| 35 | + * (error: { message: string }) => this.logError(error.message) |
| 36 | + * ) |
| 37 | + * ) |
| 38 | + * ) |
| 39 | + * ); |
| 40 | + * }); |
12 | 41 | *
|
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: { message: string }) => this.logError(error.message), |
21 |
| - * )))); |
22 |
| - * }); |
| 42 | + * readonly loadUsers = this.effect<void>((trigger$) => { |
| 43 | + * return trigger$.pipe( |
| 44 | + * tap(() => this.patchState({ loading: true })), |
| 45 | + * exhaustMap(() => |
| 46 | + * this.usersService.getAll().pipe( |
| 47 | + * tapResponse({ |
| 48 | + * next: (users) => this.patchState({ users }), |
| 49 | + * error: (error: HttpErrorResponse) => this.logError(error.message), |
| 50 | + * finalize: () => this.patchState({ loading: false }), |
| 51 | + * }) |
| 52 | + * ) |
| 53 | + * ) |
| 54 | + * ); |
| 55 | + * }); |
23 | 56 | * ```
|
24 | 57 | */
|
25 |
| -export function tapResponse<T, E = unknown>( |
26 |
| - nextFn: (next: T) => void, |
27 |
| - errorFn: (error: E) => void, |
28 |
| - completeFn?: () => void |
29 |
| -): (source: Observable<T>) => Observable<T> { |
| 58 | +export function tapResponse<T, E>( |
| 59 | + observerOrNext: TapResponseObserver<T, E> | ((value: T) => void), |
| 60 | + error?: (error: E) => void, |
| 61 | + complete?: () => void |
| 62 | +): (source$: Observable<T>) => Observable<T> { |
| 63 | + const observer: TapResponseObserver<T, E> = |
| 64 | + typeof observerOrNext === 'function' |
| 65 | + ? { |
| 66 | + next: observerOrNext, |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 68 | + error: error!, |
| 69 | + complete, |
| 70 | + } |
| 71 | + : observerOrNext; |
| 72 | + |
30 | 73 | return (source) =>
|
31 | 74 | source.pipe(
|
32 |
| - tap({ |
33 |
| - next: nextFn, |
34 |
| - complete: completeFn, |
35 |
| - }), |
36 |
| - catchError((e) => { |
37 |
| - errorFn(e); |
| 75 | + tap({ next: observer.next, complete: observer.complete }), |
| 76 | + catchError((error) => { |
| 77 | + observer.error(error); |
38 | 78 | return EMPTY;
|
39 |
| - }) |
| 79 | + }), |
| 80 | + observer.finalize ? finalize(observer.finalize) : (source$) => source$ |
40 | 81 | );
|
41 | 82 | }
|
0 commit comments