Front end of the Slack clone application.

exhaust.js 2.7KB

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