-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component-store): add tapResponse signature with observer object (…
- Loading branch information
1 parent
f6ce20f
commit 3a5e5d8
Showing
3 changed files
with
152 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,82 @@ | ||
import { EMPTY, Observable } from 'rxjs'; | ||
import { catchError, finalize, tap } from 'rxjs/operators'; | ||
|
||
import { catchError, tap } from 'rxjs/operators'; | ||
type TapResponseObserver<T, E> = { | ||
next: (value: T) => void; | ||
error: (error: E) => void; | ||
complete?: () => void; | ||
finalize?: () => void; | ||
}; | ||
|
||
export function tapResponse<T, E = unknown>( | ||
observer: TapResponseObserver<T, E> | ||
): (source$: Observable<T>) => Observable<T>; | ||
export function tapResponse<T, E = unknown>( | ||
next: (value: T) => void, | ||
error: (error: E) => void, | ||
complete?: () => void | ||
): (source$: Observable<T>) => Observable<T>; | ||
/** | ||
* Handles the response in ComponentStore effects in a safe way, without | ||
* additional boilerplate. | ||
* It enforces that the error case is handled and that the effect would still be | ||
* running should an error occur. | ||
* additional boilerplate. It enforces that the error case is handled and | ||
* that the effect would still be running should an error occur. | ||
* | ||
* Takes optional callbacks for `complete` and `finalize`. | ||
* | ||
* @usageNotes | ||
* | ||
* Takes an optional third argument for a `complete` callback. | ||
* ```ts | ||
* readonly dismissAlert = this.effect<Alert>((alert$) => { | ||
* return alert$.pipe( | ||
* concatMap( | ||
* (alert) => this.alertsService.dismissAlert(alert).pipe( | ||
* tapResponse( | ||
* (dismissedAlert) => this.alertDismissed(dismissedAlert), | ||
* (error: { message: string }) => this.logError(error.message) | ||
* ) | ||
* ) | ||
* ) | ||
* ); | ||
* }); | ||
* | ||
* ```typescript | ||
* readonly dismissedAlerts = this.effect<Alert>(alert$ => { | ||
* return alert$.pipe( | ||
* concatMap( | ||
* (alert) => this.alertsService.dismissAlert(alert).pipe( | ||
* tapResponse( | ||
* (dismissedAlert) => this.alertDismissed(dismissedAlert), | ||
* (error: { message: string }) => this.logError(error.message), | ||
* )))); | ||
* }); | ||
* readonly loadUsers = this.effect<void>((trigger$) => { | ||
* return trigger$.pipe( | ||
* tap(() => this.patchState({ loading: true })), | ||
* exhaustMap(() => | ||
* this.usersService.getAll().pipe( | ||
* tapResponse({ | ||
* next: (users) => this.patchState({ users }), | ||
* error: (error: HttpErrorResponse) => this.logError(error.message), | ||
* finalize: () => this.patchState({ loading: false }), | ||
* }) | ||
* ) | ||
* ) | ||
* ); | ||
* }); | ||
* ``` | ||
*/ | ||
export function tapResponse<T, E = unknown>( | ||
nextFn: (next: T) => void, | ||
errorFn: (error: E) => void, | ||
completeFn?: () => void | ||
): (source: Observable<T>) => Observable<T> { | ||
export function tapResponse<T, E>( | ||
observerOrNext: TapResponseObserver<T, E> | ((value: T) => void), | ||
error?: (error: E) => void, | ||
complete?: () => void | ||
): (source$: Observable<T>) => Observable<T> { | ||
const observer: TapResponseObserver<T, E> = | ||
typeof observerOrNext === 'function' | ||
? { | ||
next: observerOrNext, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
error: error!, | ||
complete, | ||
} | ||
: observerOrNext; | ||
|
||
return (source) => | ||
source.pipe( | ||
tap({ | ||
next: nextFn, | ||
complete: completeFn, | ||
}), | ||
catchError((e) => { | ||
errorFn(e); | ||
tap({ next: observer.next, complete: observer.complete }), | ||
catchError((error) => { | ||
observer.error(error); | ||
return EMPTY; | ||
}) | ||
}), | ||
observer.finalize ? finalize(observer.finalize) : (source$) => source$ | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters