windowTime.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /** PURE_IMPORTS_START .._Subject,.._scheduler_async,.._Subscriber,.._util_isNumeric,.._util_isScheduler 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 { async } from '../scheduler/async';
  11. import { Subscriber } from '../Subscriber';
  12. import { isNumeric } from '../util/isNumeric';
  13. import { isScheduler } from '../util/isScheduler';
  14. export function windowTime(windowTimeSpan) {
  15. var scheduler = async;
  16. var windowCreationInterval = null;
  17. var maxWindowSize = Number.POSITIVE_INFINITY;
  18. if (isScheduler(arguments[3])) {
  19. scheduler = arguments[3];
  20. }
  21. if (isScheduler(arguments[2])) {
  22. scheduler = arguments[2];
  23. }
  24. else if (isNumeric(arguments[2])) {
  25. maxWindowSize = arguments[2];
  26. }
  27. if (isScheduler(arguments[1])) {
  28. scheduler = arguments[1];
  29. }
  30. else if (isNumeric(arguments[1])) {
  31. windowCreationInterval = arguments[1];
  32. }
  33. return function windowTimeOperatorFunction(source) {
  34. return source.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler));
  35. };
  36. }
  37. var WindowTimeOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  38. function WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
  39. this.windowTimeSpan = windowTimeSpan;
  40. this.windowCreationInterval = windowCreationInterval;
  41. this.maxWindowSize = maxWindowSize;
  42. this.scheduler = scheduler;
  43. }
  44. WindowTimeOperator.prototype.call = function (subscriber, source) {
  45. return source.subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.maxWindowSize, this.scheduler));
  46. };
  47. return WindowTimeOperator;
  48. }());
  49. var CountedSubject = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  50. __extends(CountedSubject, _super);
  51. function CountedSubject() {
  52. _super.apply(this, arguments);
  53. this._numberOfNextedValues = 0;
  54. }
  55. CountedSubject.prototype.next = function (value) {
  56. this._numberOfNextedValues++;
  57. _super.prototype.next.call(this, value);
  58. };
  59. Object.defineProperty(CountedSubject.prototype, "numberOfNextedValues", {
  60. get: function () {
  61. return this._numberOfNextedValues;
  62. },
  63. enumerable: true,
  64. configurable: true
  65. });
  66. return CountedSubject;
  67. }(Subject));
  68. /**
  69. * We need this JSDoc comment for affecting ESDoc.
  70. * @ignore
  71. * @extends {Ignored}
  72. */
  73. var WindowTimeSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  74. __extends(WindowTimeSubscriber, _super);
  75. function WindowTimeSubscriber(destination, windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
  76. _super.call(this, destination);
  77. this.destination = destination;
  78. this.windowTimeSpan = windowTimeSpan;
  79. this.windowCreationInterval = windowCreationInterval;
  80. this.maxWindowSize = maxWindowSize;
  81. this.scheduler = scheduler;
  82. this.windows = [];
  83. var window = this.openWindow();
  84. if (windowCreationInterval !== null && windowCreationInterval >= 0) {
  85. var closeState = { subscriber: this, window: window, context: null };
  86. var creationState = { windowTimeSpan: windowTimeSpan, windowCreationInterval: windowCreationInterval, subscriber: this, scheduler: scheduler };
  87. this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
  88. this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
  89. }
  90. else {
  91. var timeSpanOnlyState = { subscriber: this, window: window, windowTimeSpan: windowTimeSpan };
  92. this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
  93. }
  94. }
  95. WindowTimeSubscriber.prototype._next = function (value) {
  96. var windows = this.windows;
  97. var len = windows.length;
  98. for (var i = 0; i < len; i++) {
  99. var window_1 = windows[i];
  100. if (!window_1.closed) {
  101. window_1.next(value);
  102. if (window_1.numberOfNextedValues >= this.maxWindowSize) {
  103. this.closeWindow(window_1);
  104. }
  105. }
  106. }
  107. };
  108. WindowTimeSubscriber.prototype._error = function (err) {
  109. var windows = this.windows;
  110. while (windows.length > 0) {
  111. windows.shift().error(err);
  112. }
  113. this.destination.error(err);
  114. };
  115. WindowTimeSubscriber.prototype._complete = function () {
  116. var windows = this.windows;
  117. while (windows.length > 0) {
  118. var window_2 = windows.shift();
  119. if (!window_2.closed) {
  120. window_2.complete();
  121. }
  122. }
  123. this.destination.complete();
  124. };
  125. WindowTimeSubscriber.prototype.openWindow = function () {
  126. var window = new CountedSubject();
  127. this.windows.push(window);
  128. var destination = this.destination;
  129. destination.next(window);
  130. return window;
  131. };
  132. WindowTimeSubscriber.prototype.closeWindow = function (window) {
  133. window.complete();
  134. var windows = this.windows;
  135. windows.splice(windows.indexOf(window), 1);
  136. };
  137. return WindowTimeSubscriber;
  138. }(Subscriber));
  139. function dispatchWindowTimeSpanOnly(state) {
  140. var subscriber = state.subscriber, windowTimeSpan = state.windowTimeSpan, window = state.window;
  141. if (window) {
  142. subscriber.closeWindow(window);
  143. }
  144. state.window = subscriber.openWindow();
  145. this.schedule(state, windowTimeSpan);
  146. }
  147. function dispatchWindowCreation(state) {
  148. var windowTimeSpan = state.windowTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler, windowCreationInterval = state.windowCreationInterval;
  149. var window = subscriber.openWindow();
  150. var action = this;
  151. var context = { action: action, subscription: null };
  152. var timeSpanState = { subscriber: subscriber, window: window, context: context };
  153. context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
  154. action.add(context.subscription);
  155. action.schedule(state, windowCreationInterval);
  156. }
  157. function dispatchWindowClose(state) {
  158. var subscriber = state.subscriber, window = state.window, context = state.context;
  159. if (context && context.action && context.subscription) {
  160. context.action.remove(context.subscription);
  161. }
  162. subscriber.closeWindow(window);
  163. }
  164. //# sourceMappingURL=windowTime.js.map