Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

expand.ts 6.2KB

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