window.js 4.2 KB

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