window.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /** PURE_IMPORTS_START .._Subject,.._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subject } from '../Subject';
  10. import { OuterSubscriber } from '../OuterSubscriber';
  11. import { subscribeToResult } from '../util/subscribeToResult';
  12. /**
  13. * Branch out the source Observable values as a nested Observable whenever
  14. * `windowBoundaries` emits.
  15. *
  16. * <span class="informal">It's like {@link buffer}, but emits a nested Observable
  17. * instead of an array.</span>
  18. *
  19. * <img src="./img/window.png" width="100%">
  20. *
  21. * Returns an Observable that emits windows of items it collects from the source
  22. * Observable. The output Observable emits connected, non-overlapping
  23. * windows. It emits the current window and opens a new one whenever the
  24. * Observable `windowBoundaries` emits an item. Because each window is an
  25. * Observable, the output is a higher-order Observable.
  26. *
  27. * @example <caption>In every window of 1 second each, emit at most 2 click events</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var interval = Rx.Observable.interval(1000);
  30. * var result = clicks.window(interval)
  31. * .map(win => win.take(2)) // each window has at most 2 emissions
  32. * .mergeAll(); // flatten the Observable-of-Observables
  33. * result.subscribe(x => console.log(x));
  34. *
  35. * @see {@link windowCount}
  36. * @see {@link windowTime}
  37. * @see {@link windowToggle}
  38. * @see {@link windowWhen}
  39. * @see {@link buffer}
  40. *
  41. * @param {Observable<any>} windowBoundaries An Observable that completes the
  42. * previous window and starts a new window.
  43. * @return {Observable<Observable<T>>} An Observable of windows, which are
  44. * Observables emitting values of the source Observable.
  45. * @method window
  46. * @owner Observable
  47. */
  48. export function window(windowBoundaries) {
  49. return function windowOperatorFunction(source) {
  50. return source.lift(new WindowOperator(windowBoundaries));
  51. };
  52. }
  53. var WindowOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  54. function WindowOperator(windowBoundaries) {
  55. this.windowBoundaries = windowBoundaries;
  56. }
  57. WindowOperator.prototype.call = function (subscriber, source) {
  58. var windowSubscriber = new WindowSubscriber(subscriber);
  59. var sourceSubscription = source.subscribe(windowSubscriber);
  60. if (!sourceSubscription.closed) {
  61. windowSubscriber.add(subscribeToResult(windowSubscriber, this.windowBoundaries));
  62. }
  63. return sourceSubscription;
  64. };
  65. return WindowOperator;
  66. }());
  67. /**
  68. * We need this JSDoc comment for affecting ESDoc.
  69. * @ignore
  70. * @extends {Ignored}
  71. */
  72. var WindowSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  73. __extends(WindowSubscriber, _super);
  74. function WindowSubscriber(destination) {
  75. _super.call(this, destination);
  76. this.window = new Subject();
  77. destination.next(this.window);
  78. }
  79. WindowSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  80. this.openWindow();
  81. };
  82. WindowSubscriber.prototype.notifyError = function (error, innerSub) {
  83. this._error(error);
  84. };
  85. WindowSubscriber.prototype.notifyComplete = function (innerSub) {
  86. this._complete();
  87. };
  88. WindowSubscriber.prototype._next = function (value) {
  89. this.window.next(value);
  90. };
  91. WindowSubscriber.prototype._error = function (err) {
  92. this.window.error(err);
  93. this.destination.error(err);
  94. };
  95. WindowSubscriber.prototype._complete = function () {
  96. this.window.complete();
  97. this.destination.complete();
  98. };
  99. /** @deprecated internal use only */ WindowSubscriber.prototype._unsubscribe = function () {
  100. this.window = null;
  101. };
  102. WindowSubscriber.prototype.openWindow = function () {
  103. var prevWindow = this.window;
  104. if (prevWindow) {
  105. prevWindow.complete();
  106. }
  107. var destination = this.destination;
  108. var newWindow = this.window = new Subject();
  109. destination.next(newWindow);
  110. };
  111. return WindowSubscriber;
  112. }(OuterSubscriber));
  113. //# sourceMappingURL=window.js.map