Created
May 18, 2019 21:21
-
-
Save glebmachine/9d570d0ba257e0267f77cbff8e2ecda3 to your computer and use it in GitHub Desktop.
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
export class DataServiceError<T> { | |
constructor(public error: any, public requestData: T) {} | |
} | |
// Function of additional success actions | |
// that returns a function that returns | |
// an observable of ngrx action(s) from DataService method observable | |
export const toAction = (...actions: Action[]) => <T>( | |
source: Observable<T>, | |
successAction: new (data: T) => Action, | |
errorAction: new (err: DataServiceError<T>) => Action | |
) => | |
source.pipe( | |
mergeMap((data: T) => [new successAction(data), ...actions]), | |
catchError((err: DataServiceError<T>) => of(new errorAction(err))) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment