switchMapTo.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /** PURE_IMPORTS_START .._operators_switchMapTo PURE_IMPORTS_END */
  2. import { switchMapTo as higherOrder } from '../operators/switchMapTo';
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Projects each source value to the same Observable which is flattened multiple
  6. * times with {@link switch} in the output Observable.
  7. *
  8. * <span class="informal">It's like {@link switchMap}, but maps each value
  9. * always to the same inner Observable.</span>
  10. *
  11. * <img src="./img/switchMapTo.png" width="100%">
  12. *
  13. * Maps each source value to the given Observable `innerObservable` regardless
  14. * of the source value, and then flattens those resulting Observables into one
  15. * single Observable, which is the output Observable. The output Observables
  16. * emits values only from the most recently emitted instance of
  17. * `innerObservable`.
  18. *
  19. * @example <caption>Rerun an interval Observable on every click event</caption>
  20. * var clicks = Rx.Observable.fromEvent(document, 'click');
  21. * var result = clicks.switchMapTo(Rx.Observable.interval(1000));
  22. * result.subscribe(x => console.log(x));
  23. *
  24. * @see {@link concatMapTo}
  25. * @see {@link switch}
  26. * @see {@link switchMap}
  27. * @see {@link mergeMapTo}
  28. *
  29. * @param {ObservableInput} innerObservable An Observable to replace each value from
  30. * the source Observable.
  31. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  32. * A function to produce the value on the output Observable based on the values
  33. * and the indices of the source (outer) emission and the inner Observable
  34. * emission. The arguments passed to this function are:
  35. * - `outerValue`: the value that came from the source
  36. * - `innerValue`: the value that came from the projected Observable
  37. * - `outerIndex`: the "index" of the value that came from the source
  38. * - `innerIndex`: the "index" of the value from the projected Observable
  39. * @return {Observable} An Observable that emits items from the given
  40. * `innerObservable` (and optionally transformed through `resultSelector`) every
  41. * time a value is emitted on the source Observable, and taking only the values
  42. * from the most recently projected inner Observable.
  43. * @method switchMapTo
  44. * @owner Observable
  45. */
  46. export function switchMapTo(innerObservable, resultSelector) {
  47. return higherOrder(innerObservable, resultSelector)(this);
  48. }
  49. //# sourceMappingURL=switchMapTo.js.map