|
| 1 | +import { of, EMPTY } from 'rxjs'; |
| 2 | +import { concatMap, withLatestFrom, tap, catchError, finalize } from 'rxjs/operators'; |
| 3 | + |
| 4 | +/** |
| 5 | + * `concatLatestFrom` combines the source value |
| 6 | + * and the last available value from a lazily evaluated Observable |
| 7 | + * in a new array |
| 8 | + * |
| 9 | + * @usageNotes |
| 10 | + * |
| 11 | + * Select the active customer from the NgRx Store |
| 12 | + * |
| 13 | + * ```ts |
| 14 | + * import { concatLatestFrom } from '@ngrx/effects'; |
| 15 | + * import * as fromCustomers from '../customers'; |
| 16 | + * |
| 17 | + * this.actions$.pipe( |
| 18 | + * concatLatestFrom(() => this.store.select(fromCustomers.selectActiveCustomer)) |
| 19 | + * ) |
| 20 | + * ``` |
| 21 | + * |
| 22 | + * Select a customer from the NgRx Store by its id that is available on the action |
| 23 | + * |
| 24 | + * ```ts |
| 25 | + * import { concatLatestFrom } from '@ngrx/effects'; |
| 26 | + * import * fromCustomers from '../customers'; |
| 27 | + * |
| 28 | + * this.actions$.pipe( |
| 29 | + * concatLatestFrom((action) => this.store.select(fromCustomers.selectCustomer(action.customerId))) |
| 30 | + * ) |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +function concatLatestFrom(observablesFactory) { |
| 34 | + return concatMap((value) => { |
| 35 | + const observables = observablesFactory(value); |
| 36 | + const observablesAsArray = Array.isArray(observables) |
| 37 | + ? observables |
| 38 | + : [observables]; |
| 39 | + return of(value).pipe(withLatestFrom(...observablesAsArray)); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Handles the response in ComponentStore effects in a safe way, without |
| 45 | + * additional boilerplate. It enforces that the error case is handled and |
| 46 | + * that the effect would still be running should an error occur. |
| 47 | + * |
| 48 | + * Takes optional callbacks for `complete` and `finalize`. |
| 49 | + * |
| 50 | + * @usageNotes |
| 51 | + * |
| 52 | + * ```ts |
| 53 | + * readonly dismissAlert = this.effect<Alert>((alert$) => { |
| 54 | + * return alert$.pipe( |
| 55 | + * concatMap( |
| 56 | + * (alert) => this.alertsService.dismissAlert(alert).pipe( |
| 57 | + * tapResponse( |
| 58 | + * (dismissedAlert) => this.alertDismissed(dismissedAlert), |
| 59 | + * (error: { message: string }) => this.logError(error.message) |
| 60 | + * ) |
| 61 | + * ) |
| 62 | + * ) |
| 63 | + * ); |
| 64 | + * }); |
| 65 | + * |
| 66 | + * readonly loadUsers = this.effect<void>((trigger$) => { |
| 67 | + * return trigger$.pipe( |
| 68 | + * tap(() => this.patchState({ loading: true })), |
| 69 | + * exhaustMap(() => |
| 70 | + * this.usersService.getAll().pipe( |
| 71 | + * tapResponse({ |
| 72 | + * next: (users) => this.patchState({ users }), |
| 73 | + * error: (error: HttpErrorResponse) => this.logError(error.message), |
| 74 | + * finalize: () => this.patchState({ loading: false }), |
| 75 | + * }) |
| 76 | + * ) |
| 77 | + * ) |
| 78 | + * ); |
| 79 | + * }); |
| 80 | + * ``` |
| 81 | + */ |
| 82 | +function tapResponse(observerOrNext, error, complete) { |
| 83 | + const observer = typeof observerOrNext === 'function' |
| 84 | + ? { |
| 85 | + next: observerOrNext, |
| 86 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 87 | + error: error, |
| 88 | + complete, |
| 89 | + } |
| 90 | + : observerOrNext; |
| 91 | + return (source) => source.pipe(tap({ next: observer.next, complete: observer.complete }), catchError((error) => { |
| 92 | + observer.error(error); |
| 93 | + return EMPTY; |
| 94 | + }), observer.finalize ? finalize(observer.finalize) : (source$) => source$); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * DO NOT EDIT |
| 99 | + * |
| 100 | + * This file is automatically generated at build |
| 101 | + */ |
| 102 | + |
| 103 | +/** |
| 104 | + * Generated bundle index. Do not edit. |
| 105 | + */ |
| 106 | + |
| 107 | +export { concatLatestFrom, tapResponse }; |
| 108 | +//# sourceMappingURL=ngrx-operators.mjs.map |
0 commit comments