exhaustMap

signature: exhaustMap(project: function, resultSelector: function): Observable

Map to inner observable, ignore other values until that observable completes.

exhaustMap - 图1

Examples

Example 1: exhaustMap with interval

( Stackblitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { merge } from 'rxjs/observable/merge';
  3. import { of } from 'rxjs/observable/of';
  4. import { delay, take, exhaustMap } from 'rxjs/operators';
  5. const sourceInterval = interval(1000);
  6. const delayedInterval = sourceInterval.pipe(delay(10), take(4));
  7. const exhaustSub = merge(
  8. // delay 10ms, then start interval emitting 4 values
  9. delayedInterval,
  10. // emit immediately
  11. of(true)
  12. )
  13. .pipe(exhaustMap(_ => sourceInterval.pipe(take(5))))
  14. /*
  15. * The first emitted value (of(true)) will be mapped
  16. * to an interval observable emitting 1 value every
  17. * second, completing after 5.
  18. * Because the emissions from the delayed interval
  19. * fall while this observable is still active they will be ignored.
  20. *
  21. * Contrast this with concatMap which would queue,
  22. * switchMap which would switch to a new inner observable each emission,
  23. * and mergeMap which would maintain a new subscription for each emitted value.
  24. */
  25. // output: 0, 1, 2, 3, 4
  26. .subscribe(val => console.log(val));
Example 2: Another exhaustMap with interval

( Stackblitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { exhaustMap, tap, take } from 'rxjs/operators';
  3. const firstInterval = interval(1000).pipe(take(10));
  4. const secondInterval = interval(1000).pipe(take(2));
  5. const exhaustSub = firstInterval
  6. .pipe(
  7. exhaustMap(f => {
  8. console.log(`Emission Corrected of first interval: ${f}`);
  9. return secondInterval;
  10. })
  11. )
  12. /*
  13. When we subscribed to the first interval, it starts to emit a values (starting 0).
  14. This value is mapped to the second interval which then begins to emit (starting 0).
  15. While the second interval is active, values from the first interval are ignored.
  16. We can see this when firstInterval emits number 3,6, and so on...
  17. Output:
  18. Emission of first interval: 0
  19. 0
  20. 1
  21. Emission of first interval: 3
  22. 0
  23. 1
  24. Emission of first interval: 6
  25. 0
  26. 1
  27. Emission of first interval: 9
  28. 0
  29. 1
  30. */
  31. .subscribe(s => console.log(s));

Outside Examples

exhaustMap for login effect in @ngrx example app

(
Source
)

  1. @Effect()
  2. login$ = this.actions$.pipe(
  3. ofType(AuthActionTypes.Login),
  4. map((action: Login) => action.payload),
  5. exhaustMap((auth: Authenticate) =>
  6. this.authService
  7. .login(auth)
  8. .pipe(
  9. map(user => new LoginSuccess({ user })),
  10. catchError(error => of(new LoginFailure(error)))
  11. )
  12. )
  13. );

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/exhaustMap.ts