windowCount.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /** PURE_IMPORTS_START .._Subscriber,.._Subject 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 { Subscriber } from '../Subscriber';
  10. import { Subject } from '../Subject';
  11. /**
  12. * Branch out the source Observable values as a nested Observable with each
  13. * nested Observable emitting at most `windowSize` values.
  14. *
  15. * <span class="informal">It's like {@link bufferCount}, but emits a nested
  16. * Observable instead of an array.</span>
  17. *
  18. * <img src="./img/windowCount.png" width="100%">
  19. *
  20. * Returns an Observable that emits windows of items it collects from the source
  21. * Observable. The output Observable emits windows every `startWindowEvery`
  22. * items, each containing no more than `windowSize` items. When the source
  23. * Observable completes or encounters an error, the output Observable emits
  24. * the current window and propagates the notification from the source
  25. * Observable. If `startWindowEvery` is not provided, then new windows are
  26. * started immediately at the start of the source and when each window completes
  27. * with size `windowSize`.
  28. *
  29. * @example <caption>Ignore every 3rd click event, starting from the first one</caption>
  30. * var clicks = Rx.Observable.fromEvent(document, 'click');
  31. * var result = clicks.windowCount(3)
  32. * .map(win => win.skip(1)) // skip first of every 3 clicks
  33. * .mergeAll(); // flatten the Observable-of-Observables
  34. * result.subscribe(x => console.log(x));
  35. *
  36. * @example <caption>Ignore every 3rd click event, starting from the third one</caption>
  37. * var clicks = Rx.Observable.fromEvent(document, 'click');
  38. * var result = clicks.windowCount(2, 3)
  39. * .mergeAll(); // flatten the Observable-of-Observables
  40. * result.subscribe(x => console.log(x));
  41. *
  42. * @see {@link window}
  43. * @see {@link windowTime}
  44. * @see {@link windowToggle}
  45. * @see {@link windowWhen}
  46. * @see {@link bufferCount}
  47. *
  48. * @param {number} windowSize The maximum number of values emitted by each
  49. * window.
  50. * @param {number} [startWindowEvery] Interval at which to start a new window.
  51. * For example if `startWindowEvery` is `2`, then a new window will be started
  52. * on every other value from the source. A new window is started at the
  53. * beginning of the source by default.
  54. * @return {Observable<Observable<T>>} An Observable of windows, which in turn
  55. * are Observable of values.
  56. * @method windowCount
  57. * @owner Observable
  58. */
  59. export function windowCount(windowSize, startWindowEvery) {
  60. if (startWindowEvery === void 0) {
  61. startWindowEvery = 0;
  62. }
  63. return function windowCountOperatorFunction(source) {
  64. return source.lift(new WindowCountOperator(windowSize, startWindowEvery));
  65. };
  66. }
  67. var WindowCountOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  68. function WindowCountOperator(windowSize, startWindowEvery) {
  69. this.windowSize = windowSize;
  70. this.startWindowEvery = startWindowEvery;
  71. }
  72. WindowCountOperator.prototype.call = function (subscriber, source) {
  73. return source.subscribe(new WindowCountSubscriber(subscriber, this.windowSize, this.startWindowEvery));
  74. };
  75. return WindowCountOperator;
  76. }());
  77. /**
  78. * We need this JSDoc comment for affecting ESDoc.
  79. * @ignore
  80. * @extends {Ignored}
  81. */
  82. var WindowCountSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  83. __extends(WindowCountSubscriber, _super);
  84. function WindowCountSubscriber(destination, windowSize, startWindowEvery) {
  85. _super.call(this, destination);
  86. this.destination = destination;
  87. this.windowSize = windowSize;
  88. this.startWindowEvery = startWindowEvery;
  89. this.windows = [new Subject()];
  90. this.count = 0;
  91. destination.next(this.windows[0]);
  92. }
  93. WindowCountSubscriber.prototype._next = function (value) {
  94. var startWindowEvery = (this.startWindowEvery > 0) ? this.startWindowEvery : this.windowSize;
  95. var destination = this.destination;
  96. var windowSize = this.windowSize;
  97. var windows = this.windows;
  98. var len = windows.length;
  99. for (var i = 0; i < len && !this.closed; i++) {
  100. windows[i].next(value);
  101. }
  102. var c = this.count - windowSize + 1;
  103. if (c >= 0 && c % startWindowEvery === 0 && !this.closed) {
  104. windows.shift().complete();
  105. }
  106. if (++this.count % startWindowEvery === 0 && !this.closed) {
  107. var window_1 = new Subject();
  108. windows.push(window_1);
  109. destination.next(window_1);
  110. }
  111. };
  112. WindowCountSubscriber.prototype._error = function (err) {
  113. var windows = this.windows;
  114. if (windows) {
  115. while (windows.length > 0 && !this.closed) {
  116. windows.shift().error(err);
  117. }
  118. }
  119. this.destination.error(err);
  120. };
  121. WindowCountSubscriber.prototype._complete = function () {
  122. var windows = this.windows;
  123. if (windows) {
  124. while (windows.length > 0 && !this.closed) {
  125. windows.shift().complete();
  126. }
  127. }
  128. this.destination.complete();
  129. };
  130. /** @deprecated internal use only */ WindowCountSubscriber.prototype._unsubscribe = function () {
  131. this.count = 0;
  132. this.windows = null;
  133. };
  134. return WindowCountSubscriber;
  135. }(Subscriber));
  136. //# sourceMappingURL=windowCount.js.map