switchMap.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /** PURE_IMPORTS_START .._operators_switchMap PURE_IMPORTS_END */
  2. import { switchMap as higherOrderSwitchMap } from '../operators/switchMap';
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Projects each source value to an Observable which is merged in the output
  6. * Observable, emitting values only from the most recently projected Observable.
  7. *
  8. * <span class="informal">Maps each value to an Observable, then flattens all of
  9. * these inner Observables using {@link switch}.</span>
  10. *
  11. * <img src="./img/switchMap.png" width="100%">
  12. *
  13. * Returns an Observable that emits items based on applying a function that you
  14. * supply to each item emitted by the source Observable, where that function
  15. * returns an (so-called "inner") Observable. Each time it observes one of these
  16. * inner Observables, the output Observable begins emitting the items emitted by
  17. * that inner Observable. When a new inner Observable is emitted, `switchMap`
  18. * stops emitting items from the earlier-emitted inner Observable and begins
  19. * emitting items from the new one. It continues to behave like this for
  20. * subsequent inner Observables.
  21. *
  22. * @example <caption>Rerun an interval Observable on every click event</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var result = clicks.switchMap((ev) => Rx.Observable.interval(1000));
  25. * result.subscribe(x => console.log(x));
  26. *
  27. * @see {@link concatMap}
  28. * @see {@link exhaustMap}
  29. * @see {@link mergeMap}
  30. * @see {@link switch}
  31. * @see {@link switchMapTo}
  32. *
  33. * @param {function(value: T, ?index: number): ObservableInput} project A function
  34. * that, when applied to an item emitted by the source Observable, returns an
  35. * Observable.
  36. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  37. * A function to produce the value on the output Observable based on the values
  38. * and the indices of the source (outer) emission and the inner Observable
  39. * emission. The arguments passed to this function are:
  40. * - `outerValue`: the value that came from the source
  41. * - `innerValue`: the value that came from the projected Observable
  42. * - `outerIndex`: the "index" of the value that came from the source
  43. * - `innerIndex`: the "index" of the value from the projected Observable
  44. * @return {Observable} An Observable that emits the result of applying the
  45. * projection function (and the optional `resultSelector`) to each item emitted
  46. * by the source Observable and taking only the values from the most recently
  47. * projected inner Observable.
  48. * @method switchMap
  49. * @owner Observable
  50. */
  51. export function switchMap(project, resultSelector) {
  52. return higherOrderSwitchMap(project, resultSelector)(this);
  53. }
  54. //# sourceMappingURL=switchMap.js.map