expand.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { tryCatch } from '../util/tryCatch';
  2. import { errorObject } from '../util/errorObject';
  3. import { OuterSubscriber } from '../OuterSubscriber';
  4. import { subscribeToResult } from '../util/subscribeToResult';
  5. /* tslint:enable:max-line-length */
  6. /**
  7. * Recursively projects each source value to an Observable which is merged in
  8. * the output Observable.
  9. *
  10. * <span class="informal">It's similar to {@link mergeMap}, but applies the
  11. * projection function to every source value as well as every output value.
  12. * It's recursive.</span>
  13. *
  14. * <img src="./img/expand.png" width="100%">
  15. *
  16. * Returns an Observable that emits items based on applying a function that you
  17. * supply to each item emitted by the source Observable, where that function
  18. * returns an Observable, and then merging those resulting Observables and
  19. * emitting the results of this merger. *Expand* will re-emit on the output
  20. * Observable every source value. Then, each output value is given to the
  21. * `project` function which returns an inner Observable to be merged on the
  22. * output Observable. Those output values resulting from the projection are also
  23. * given to the `project` function to produce new output values. This is how
  24. * *expand* behaves recursively.
  25. *
  26. * @example <caption>Start emitting the powers of two on every click, at most 10 of them</caption>
  27. * var clicks = Rx.Observable.fromEvent(document, 'click');
  28. * var powersOfTwo = clicks
  29. * .mapTo(1)
  30. * .expand(x => Rx.Observable.of(2 * x).delay(1000))
  31. * .take(10);
  32. * powersOfTwo.subscribe(x => console.log(x));
  33. *
  34. * @see {@link mergeMap}
  35. * @see {@link mergeScan}
  36. *
  37. * @param {function(value: T, index: number) => Observable} project A function
  38. * that, when applied to an item emitted by the source or the output Observable,
  39. * returns an Observable.
  40. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  41. * Observables being subscribed to concurrently.
  42. * @param {Scheduler} [scheduler=null] The IScheduler to use for subscribing to
  43. * each projected inner Observable.
  44. * @return {Observable} An Observable that emits the source values and also
  45. * result of applying the projection function to each value emitted on the
  46. * output Observable and and merging the results of the Observables obtained
  47. * from this transformation.
  48. * @method expand
  49. * @owner Observable
  50. */
  51. export function expand(project, concurrent = Number.POSITIVE_INFINITY, scheduler = undefined) {
  52. concurrent = (concurrent || 0) < 1 ? Number.POSITIVE_INFINITY : concurrent;
  53. return (source) => source.lift(new ExpandOperator(project, concurrent, scheduler));
  54. }
  55. export class ExpandOperator {
  56. constructor(project, concurrent, scheduler) {
  57. this.project = project;
  58. this.concurrent = concurrent;
  59. this.scheduler = scheduler;
  60. }
  61. call(subscriber, source) {
  62. return source.subscribe(new ExpandSubscriber(subscriber, this.project, this.concurrent, this.scheduler));
  63. }
  64. }
  65. /**
  66. * We need this JSDoc comment for affecting ESDoc.
  67. * @ignore
  68. * @extends {Ignored}
  69. */
  70. export class ExpandSubscriber extends OuterSubscriber {
  71. constructor(destination, project, concurrent, scheduler) {
  72. super(destination);
  73. this.project = project;
  74. this.concurrent = concurrent;
  75. this.scheduler = scheduler;
  76. this.index = 0;
  77. this.active = 0;
  78. this.hasCompleted = false;
  79. if (concurrent < Number.POSITIVE_INFINITY) {
  80. this.buffer = [];
  81. }
  82. }
  83. static dispatch(arg) {
  84. const { subscriber, result, value, index } = arg;
  85. subscriber.subscribeToProjection(result, value, index);
  86. }
  87. _next(value) {
  88. const destination = this.destination;
  89. if (destination.closed) {
  90. this._complete();
  91. return;
  92. }
  93. const index = this.index++;
  94. if (this.active < this.concurrent) {
  95. destination.next(value);
  96. let result = tryCatch(this.project)(value, index);
  97. if (result === errorObject) {
  98. destination.error(errorObject.e);
  99. }
  100. else if (!this.scheduler) {
  101. this.subscribeToProjection(result, value, index);
  102. }
  103. else {
  104. const state = { subscriber: this, result, value, index };
  105. this.add(this.scheduler.schedule(ExpandSubscriber.dispatch, 0, state));
  106. }
  107. }
  108. else {
  109. this.buffer.push(value);
  110. }
  111. }
  112. subscribeToProjection(result, value, index) {
  113. this.active++;
  114. this.add(subscribeToResult(this, result, value, index));
  115. }
  116. _complete() {
  117. this.hasCompleted = true;
  118. if (this.hasCompleted && this.active === 0) {
  119. this.destination.complete();
  120. }
  121. }
  122. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  123. this._next(innerValue);
  124. }
  125. notifyComplete(innerSub) {
  126. const buffer = this.buffer;
  127. this.remove(innerSub);
  128. this.active--;
  129. if (buffer && buffer.length > 0) {
  130. this._next(buffer.shift());
  131. }
  132. if (this.hasCompleted && this.active === 0) {
  133. this.destination.complete();
  134. }
  135. }
  136. }
  137. //# sourceMappingURL=expand.js.map