windowCount.js 5.3 KB

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