expand.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Recursively projects each source value to an Observable which is merged in
  7. * the output Observable.
  8. *
  9. * <span class="informal">It's similar to {@link mergeMap}, but applies the
  10. * projection function to every source value as well as every output value.
  11. * It's recursive.</span>
  12. *
  13. * <img src="./img/expand.png" width="100%">
  14. *
  15. * Returns an Observable that emits items based on applying a function that you
  16. * supply to each item emitted by the source Observable, where that function
  17. * returns an Observable, and then merging those resulting Observables and
  18. * emitting the results of this merger. *Expand* will re-emit on the output
  19. * Observable every source value. Then, each output value is given to the
  20. * `project` function which returns an inner Observable to be merged on the
  21. * output Observable. Those output values resulting from the projection are also
  22. * given to the `project` function to produce new output values. This is how
  23. * *expand* behaves recursively.
  24. *
  25. * @example <caption>Start emitting the powers of two on every click, at most 10 of them</caption>
  26. * var clicks = Rx.Observable.fromEvent(document, 'click');
  27. * var powersOfTwo = clicks
  28. * .mapTo(1)
  29. * .expand(x => Rx.Observable.of(2 * x).delay(1000))
  30. * .take(10);
  31. * powersOfTwo.subscribe(x => console.log(x));
  32. *
  33. * @see {@link mergeMap}
  34. * @see {@link mergeScan}
  35. *
  36. * @param {function(value: T, index: number) => Observable} project A function
  37. * that, when applied to an item emitted by the source or the output Observable,
  38. * returns an Observable.
  39. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  40. * Observables being subscribed to concurrently.
  41. * @param {Scheduler} [scheduler=null] The IScheduler to use for subscribing to
  42. * each projected inner Observable.
  43. * @return {Observable} An Observable that emits the source values and also
  44. * result of applying the projection function to each value emitted on the
  45. * output Observable and and merging the results of the Observables obtained
  46. * from this transformation.
  47. * @method expand
  48. * @owner Observable
  49. */
  50. function expand(project, concurrent, scheduler) {
  51. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  52. if (scheduler === void 0) { scheduler = undefined; }
  53. concurrent = (concurrent || 0) < 1 ? Number.POSITIVE_INFINITY : concurrent;
  54. return operators_1.expand(project, concurrent, scheduler)(this);
  55. }
  56. exports.expand = expand;
  57. //# sourceMappingURL=expand.js.map