concatMapTo.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var concatMap_1 = require('./concatMap');
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Projects each source value to the same Observable which is merged multiple
  6. * times in a serialized fashion on the output Observable.
  7. *
  8. * <span class="informal">It's like {@link concatMap}, but maps each value
  9. * always to the same inner Observable.</span>
  10. *
  11. * <img src="./img/concatMapTo.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. Each new `innerObservable`
  16. * instance emitted on the output Observable is concatenated with the previous
  17. * `innerObservable` instance.
  18. *
  19. * __Warning:__ if source values arrive endlessly and faster than their
  20. * corresponding inner Observables can complete, it will result in memory issues
  21. * as inner Observables amass in an unbounded buffer waiting for their turn to
  22. * be subscribed to.
  23. *
  24. * Note: `concatMapTo` is equivalent to `mergeMapTo` with concurrency parameter
  25. * set to `1`.
  26. *
  27. * @example <caption>For each click event, tick every second from 0 to 3, with no concurrency</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = clicks.concatMapTo(Rx.Observable.interval(1000).take(4));
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * // Results in the following:
  33. * // (results are not concurrent)
  34. * // For every click on the "document" it will emit values 0 to 3 spaced
  35. * // on a 1000ms interval
  36. * // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3
  37. *
  38. * @see {@link concat}
  39. * @see {@link concatAll}
  40. * @see {@link concatMap}
  41. * @see {@link mergeMapTo}
  42. * @see {@link switchMapTo}
  43. *
  44. * @param {ObservableInput} innerObservable An Observable to replace each value from
  45. * the source Observable.
  46. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  47. * A function to produce the value on the output Observable based on the values
  48. * and the indices of the source (outer) emission and the inner Observable
  49. * emission. The arguments passed to this function are:
  50. * - `outerValue`: the value that came from the source
  51. * - `innerValue`: the value that came from the projected Observable
  52. * - `outerIndex`: the "index" of the value that came from the source
  53. * - `innerIndex`: the "index" of the value from the projected Observable
  54. * @return {Observable} An observable of values merged together by joining the
  55. * passed observable with itself, one after the other, for each value emitted
  56. * from the source.
  57. * @method concatMapTo
  58. * @owner Observable
  59. */
  60. function concatMapTo(innerObservable, resultSelector) {
  61. return concatMap_1.concatMap(function () { return innerObservable; }, resultSelector);
  62. }
  63. exports.concatMapTo = concatMapTo;
  64. //# sourceMappingURL=concatMapTo.js.map