concatMap.js 2.9 KB

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