windowTime.js 6.6 KB

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