windowCount.js 4.6 KB

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