Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

windowTime.js 7.0KB

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