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

exhaust.ts 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { ObservableInput, OperatorFunction, TeardownLogic } from '../types';
  5. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  6. export function exhaust<T>(): OperatorFunction<ObservableInput<T>, T>;
  7. export function exhaust<R>(): OperatorFunction<any, R>;
  8. /**
  9. * Converts a higher-order Observable into a first-order Observable by dropping
  10. * inner Observables while the previous inner Observable has not yet completed.
  11. *
  12. * <span class="informal">Flattens an Observable-of-Observables by dropping the
  13. * next inner Observables while the current inner is still executing.</span>
  14. *
  15. * ![](exhaust.png)
  16. *
  17. * `exhaust` subscribes to an Observable that emits Observables, also known as a
  18. * higher-order Observable. Each time it observes one of these emitted inner
  19. * Observables, the output Observable begins emitting the items emitted by that
  20. * inner Observable. So far, it behaves like {@link mergeAll}. However,
  21. * `exhaust` ignores every new inner Observable if the previous Observable has
  22. * not yet completed. Once that one completes, it will accept and flatten the
  23. * next inner Observable and repeat this process.
  24. *
  25. * ## Example
  26. * Run a finite timer for each click, only if there is no currently active timer
  27. * ```ts
  28. * import { fromEvent, interval } from 'rxjs';
  29. * import { exhaust, map, take } from 'rxjs/operators';
  30. *
  31. * const clicks = fromEvent(document, 'click');
  32. * const higherOrder = clicks.pipe(
  33. * map((ev) => interval(1000).pipe(take(5))),
  34. * );
  35. * const result = higherOrder.pipe(exhaust());
  36. * result.subscribe(x => console.log(x));
  37. * ```
  38. *
  39. * @see {@link combineAll}
  40. * @see {@link concatAll}
  41. * @see {@link switchAll}
  42. * @see {@link switchMap}
  43. * @see {@link mergeAll}
  44. * @see {@link exhaustMap}
  45. * @see {@link zipAll}
  46. *
  47. * @return {Observable} An Observable that takes a source of Observables and propagates the first observable
  48. * exclusively until it completes before subscribing to the next.
  49. * @method exhaust
  50. * @owner Observable
  51. */
  52. export function exhaust<T>(): OperatorFunction<any, T> {
  53. return (source: Observable<T>) => source.lift(new SwitchFirstOperator<T>());
  54. }
  55. class SwitchFirstOperator<T> implements Operator<T, T> {
  56. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  57. return source.subscribe(new SwitchFirstSubscriber(subscriber));
  58. }
  59. }
  60. /**
  61. * We need this JSDoc comment for affecting ESDoc.
  62. * @ignore
  63. * @extends {Ignored}
  64. */
  65. class SwitchFirstSubscriber<T> extends SimpleOuterSubscriber<T, T> {
  66. private hasCompleted: boolean = false;
  67. private hasSubscription: boolean = false;
  68. constructor(destination: Subscriber<T>) {
  69. super(destination);
  70. }
  71. protected _next(value: T): void {
  72. if (!this.hasSubscription) {
  73. this.hasSubscription = true;
  74. this.add(innerSubscribe(value, new SimpleInnerSubscriber(this)));
  75. }
  76. }
  77. protected _complete(): void {
  78. this.hasCompleted = true;
  79. if (!this.hasSubscription) {
  80. this.destination.complete!();
  81. }
  82. }
  83. notifyComplete(): void {
  84. this.hasSubscription = false;
  85. if (this.hasCompleted) {
  86. this.destination.complete!();
  87. }
  88. }
  89. }