UNPKG

@ngrx/operators

Version:

Shared RxJS Operators for NgRx libraries

126 lines (119 loc) 3.72 kB
import { of, EMPTY } from 'rxjs'; import { concatMap, withLatestFrom, map, catchError, tap, finalize } from 'rxjs/operators'; /** * `concatLatestFrom` combines the source value * and the last available value from a lazily evaluated Observable * in a new array * * @usageNotes * * Select the active customer from the NgRx Store * * ```ts * import { concatLatestFrom } from '@ngrx/operators'; * import * as fromCustomers from '../customers'; * * this.actions$.pipe( * concatLatestFrom(() => this.store.select(fromCustomers.selectActiveCustomer)) * ) * ``` * * Select a customer from the NgRx Store by its id that is available on the action * * ```ts * import { concatLatestFrom } from '@ngrx/operators'; * import * fromCustomers from '../customers'; * * this.actions$.pipe( * concatLatestFrom((action) => this.store.select(fromCustomers.selectCustomer(action.customerId))) * ) * ``` */ function concatLatestFrom(observablesFactory) { return concatMap((value) => { const observables = observablesFactory(value); const observablesAsArray = Array.isArray(observables) ? observables : [observables]; return of(value).pipe(withLatestFrom(...observablesAsArray)); }); } /** * `mapResponse` is a map operator with included error handling. * It is similar to `tapResponse`, but allows to map the response as well. * * The main use case is for NgRx Effects which requires an action to be dispatched. * * @usageNotes * ```ts * export const loadAllUsers = createEffect(( * actions$ = inject(Actions), * usersService = inject(UsersService) * ) => { * return actions$.pipe( * ofType(UsersPageActions.opened), * exhaustMap(() => { * return usersService.getAll().pipe( * mapResponse({ * next: (users) => UsersApiActions.usersLoadedSuccess({ users }), * error: (error) => UsersApiActions.usersLoadedFailure({ error }), * }) * ); * }) * ); * }); * ``` */ function mapResponse(observer) { return (source$) => source$.pipe(map((value) => observer.next(value)), catchError((error) => of(observer.error(error)))); } /** * 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 optional callbacks for `complete` and `finalize`. * * @usageNotes * * ```ts * readonly loadUsers = rxMethod<void>( * pipe( * tap(() => this.isLoading.set(true)), * exhaustMap(() => * this.usersService.getAll().pipe( * tapResponse({ * next: (users) => this.users.set(users), * error: (error: HttpErrorResponse) => this.logError(error.message), * finalize: () => this.isLoading.set(false), * }) * ) * ) * ) * ); * ``` */ function tapResponse(observerOrNext, error, complete) { const observer = 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: observer.next, complete: observer.complete }), catchError((error) => { observer.error(error); return EMPTY; }), observer.finalize ? finalize(observer.finalize) : (source$) => source$); } /** * DO NOT EDIT * * This file is automatically generated at build */ /** * Generated bundle index. Do not edit. */ export { concatLatestFrom, mapResponse, tapResponse }; //# sourceMappingURL=ngrx-operators.mjs.map