-
-
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 operator (#2763)
* feat(component-store): add handleResponse operator * Rename to mapResponse * rename to tapResponse
- Loading branch information
1 parent
a942ac6
commit d1873c9
Showing
2 changed files
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './component-store'; | ||
export * from './tap-response'; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { EMPTY, Observable } from 'rxjs'; | ||
|
||
import { catchError, tap } from 'rxjs/operators'; | ||
|
||
/** | ||
* 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. | ||
* | ||
* Takes an optional third argument for a `complete` callback. | ||
* | ||
* ```typescript | ||
* readonly dismissedAlerts = this.effect<Alert>(alert$ => { | ||
* return alert$.pipe( | ||
* concatMap( | ||
* (alert) => this.alertsService.dismissAlert(alert).pipe( | ||
* tapResponse( | ||
* (dismissedAlert) => this.alertDismissed(dismissedAlert), | ||
* (error) => this.logError(error), | ||
* )))); | ||
* }); | ||
* ``` | ||
*/ | ||
export function tapResponse<T>( | ||
nextFn: (next: T) => void, | ||
errorFn: (error: unknown) => void, | ||
completeFn?: () => void | ||
): (source: Observable<T>) => Observable<T> { | ||
return (source) => | ||
source.pipe( | ||
tap({ | ||
next: nextFn, | ||
error: errorFn, | ||
complete: completeFn, | ||
}), | ||
catchError(() => EMPTY) | ||
); | ||
} |