Skip to content

Instantly share code, notes, and snippets.

@glebmachine
Created May 18, 2019 21:21
Show Gist options
  • Save glebmachine/9d570d0ba257e0267f77cbff8e2ecda3 to your computer and use it in GitHub Desktop.
Save glebmachine/9d570d0ba257e0267f77cbff8e2ecda3 to your computer and use it in GitHub Desktop.
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