Skip to content

Commit

Permalink
feat(component-store): add tapResponse operator (#2763)
Browse files Browse the repository at this point in the history
* feat(component-store): add handleResponse operator

* Rename to mapResponse

* rename to tapResponse
  • Loading branch information
alex-okrushko authored Nov 7, 2020
1 parent a942ac6 commit d1873c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/component-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './component-store';
export * from './tap-response';
39 changes: 39 additions & 0 deletions modules/component-store/src/tap-response.ts
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)
);
}

0 comments on commit d1873c9

Please sign in to comment.