windowTime.js 6.4 KB

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