windowWhen.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Subject } from '../Subject';
  2. import { tryCatch } from '../util/tryCatch';
  3. import { errorObject } from '../util/errorObject';
  4. import { OuterSubscriber } from '../OuterSubscriber';
  5. import { subscribeToResult } from '../util/subscribeToResult';
  6. /**
  7. * Branch out the source Observable values as a nested Observable using a
  8. * factory function of closing Observables to determine when to start a new
  9. * window.
  10. *
  11. * <span class="informal">It's like {@link bufferWhen}, but emits a nested
  12. * Observable instead of an array.</span>
  13. *
  14. * <img src="./img/windowWhen.png" width="100%">
  15. *
  16. * Returns an Observable that emits windows of items it collects from the source
  17. * Observable. The output Observable emits connected, non-overlapping windows.
  18. * It emits the current window and opens a new one whenever the Observable
  19. * produced by the specified `closingSelector` function emits an item. The first
  20. * window is opened immediately when subscribing to the output Observable.
  21. *
  22. * @example <caption>Emit only the first two clicks events in every window of [1-5] random seconds</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var result = clicks
  25. * .windowWhen(() => Rx.Observable.interval(1000 + Math.random() * 4000))
  26. * .map(win => win.take(2)) // each window has at most 2 emissions
  27. * .mergeAll(); // flatten the Observable-of-Observables
  28. * result.subscribe(x => console.log(x));
  29. *
  30. * @see {@link window}
  31. * @see {@link windowCount}
  32. * @see {@link windowTime}
  33. * @see {@link windowToggle}
  34. * @see {@link bufferWhen}
  35. *
  36. * @param {function(): Observable} closingSelector A function that takes no
  37. * arguments and returns an Observable that signals (on either `next` or
  38. * `complete`) when to close the previous window and start a new one.
  39. * @return {Observable<Observable<T>>} An observable of windows, which in turn
  40. * are Observables.
  41. * @method windowWhen
  42. * @owner Observable
  43. */
  44. export function windowWhen(closingSelector) {
  45. return function windowWhenOperatorFunction(source) {
  46. return source.lift(new WindowOperator(closingSelector));
  47. };
  48. }
  49. class WindowOperator {
  50. constructor(closingSelector) {
  51. this.closingSelector = closingSelector;
  52. }
  53. call(subscriber, source) {
  54. return source.subscribe(new WindowSubscriber(subscriber, this.closingSelector));
  55. }
  56. }
  57. /**
  58. * We need this JSDoc comment for affecting ESDoc.
  59. * @ignore
  60. * @extends {Ignored}
  61. */
  62. class WindowSubscriber extends OuterSubscriber {
  63. constructor(destination, closingSelector) {
  64. super(destination);
  65. this.destination = destination;
  66. this.closingSelector = closingSelector;
  67. this.openWindow();
  68. }
  69. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  70. this.openWindow(innerSub);
  71. }
  72. notifyError(error, innerSub) {
  73. this._error(error);
  74. }
  75. notifyComplete(innerSub) {
  76. this.openWindow(innerSub);
  77. }
  78. _next(value) {
  79. this.window.next(value);
  80. }
  81. _error(err) {
  82. this.window.error(err);
  83. this.destination.error(err);
  84. this.unsubscribeClosingNotification();
  85. }
  86. _complete() {
  87. this.window.complete();
  88. this.destination.complete();
  89. this.unsubscribeClosingNotification();
  90. }
  91. unsubscribeClosingNotification() {
  92. if (this.closingNotification) {
  93. this.closingNotification.unsubscribe();
  94. }
  95. }
  96. openWindow(innerSub = null) {
  97. if (innerSub) {
  98. this.remove(innerSub);
  99. innerSub.unsubscribe();
  100. }
  101. const prevWindow = this.window;
  102. if (prevWindow) {
  103. prevWindow.complete();
  104. }
  105. const window = this.window = new Subject();
  106. this.destination.next(window);
  107. const closingNotifier = tryCatch(this.closingSelector)();
  108. if (closingNotifier === errorObject) {
  109. const err = errorObject.e;
  110. this.destination.error(err);
  111. this.window.error(err);
  112. }
  113. else {
  114. this.add(this.closingNotification = subscribeToResult(this, closingNotifier));
  115. }
  116. }
  117. }
  118. //# sourceMappingURL=windowWhen.js.map