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.

bufferTime.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 async_1 = require("../scheduler/async");
  17. var Subscriber_1 = require("../Subscriber");
  18. var isScheduler_1 = require("../util/isScheduler");
  19. function bufferTime(bufferTimeSpan) {
  20. var length = arguments.length;
  21. var scheduler = async_1.async;
  22. if (isScheduler_1.isScheduler(arguments[arguments.length - 1])) {
  23. scheduler = arguments[arguments.length - 1];
  24. length--;
  25. }
  26. var bufferCreationInterval = null;
  27. if (length >= 2) {
  28. bufferCreationInterval = arguments[1];
  29. }
  30. var maxBufferSize = Number.POSITIVE_INFINITY;
  31. if (length >= 3) {
  32. maxBufferSize = arguments[2];
  33. }
  34. return function bufferTimeOperatorFunction(source) {
  35. return source.lift(new BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler));
  36. };
  37. }
  38. exports.bufferTime = bufferTime;
  39. var BufferTimeOperator = (function () {
  40. function BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
  41. this.bufferTimeSpan = bufferTimeSpan;
  42. this.bufferCreationInterval = bufferCreationInterval;
  43. this.maxBufferSize = maxBufferSize;
  44. this.scheduler = scheduler;
  45. }
  46. BufferTimeOperator.prototype.call = function (subscriber, source) {
  47. return source.subscribe(new BufferTimeSubscriber(subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.maxBufferSize, this.scheduler));
  48. };
  49. return BufferTimeOperator;
  50. }());
  51. var Context = (function () {
  52. function Context() {
  53. this.buffer = [];
  54. }
  55. return Context;
  56. }());
  57. var BufferTimeSubscriber = (function (_super) {
  58. __extends(BufferTimeSubscriber, _super);
  59. function BufferTimeSubscriber(destination, bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
  60. var _this = _super.call(this, destination) || this;
  61. _this.bufferTimeSpan = bufferTimeSpan;
  62. _this.bufferCreationInterval = bufferCreationInterval;
  63. _this.maxBufferSize = maxBufferSize;
  64. _this.scheduler = scheduler;
  65. _this.contexts = [];
  66. var context = _this.openContext();
  67. _this.timespanOnly = bufferCreationInterval == null || bufferCreationInterval < 0;
  68. if (_this.timespanOnly) {
  69. var timeSpanOnlyState = { subscriber: _this, context: context, bufferTimeSpan: bufferTimeSpan };
  70. _this.add(context.closeAction = scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
  71. }
  72. else {
  73. var closeState = { subscriber: _this, context: context };
  74. var creationState = { bufferTimeSpan: bufferTimeSpan, bufferCreationInterval: bufferCreationInterval, subscriber: _this, scheduler: scheduler };
  75. _this.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
  76. _this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
  77. }
  78. return _this;
  79. }
  80. BufferTimeSubscriber.prototype._next = function (value) {
  81. var contexts = this.contexts;
  82. var len = contexts.length;
  83. var filledBufferContext;
  84. for (var i = 0; i < len; i++) {
  85. var context_1 = contexts[i];
  86. var buffer = context_1.buffer;
  87. buffer.push(value);
  88. if (buffer.length == this.maxBufferSize) {
  89. filledBufferContext = context_1;
  90. }
  91. }
  92. if (filledBufferContext) {
  93. this.onBufferFull(filledBufferContext);
  94. }
  95. };
  96. BufferTimeSubscriber.prototype._error = function (err) {
  97. this.contexts.length = 0;
  98. _super.prototype._error.call(this, err);
  99. };
  100. BufferTimeSubscriber.prototype._complete = function () {
  101. var _a = this, contexts = _a.contexts, destination = _a.destination;
  102. while (contexts.length > 0) {
  103. var context_2 = contexts.shift();
  104. destination.next(context_2.buffer);
  105. }
  106. _super.prototype._complete.call(this);
  107. };
  108. BufferTimeSubscriber.prototype._unsubscribe = function () {
  109. this.contexts = null;
  110. };
  111. BufferTimeSubscriber.prototype.onBufferFull = function (context) {
  112. this.closeContext(context);
  113. var closeAction = context.closeAction;
  114. closeAction.unsubscribe();
  115. this.remove(closeAction);
  116. if (!this.closed && this.timespanOnly) {
  117. context = this.openContext();
  118. var bufferTimeSpan = this.bufferTimeSpan;
  119. var timeSpanOnlyState = { subscriber: this, context: context, bufferTimeSpan: bufferTimeSpan };
  120. this.add(context.closeAction = this.scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
  121. }
  122. };
  123. BufferTimeSubscriber.prototype.openContext = function () {
  124. var context = new Context();
  125. this.contexts.push(context);
  126. return context;
  127. };
  128. BufferTimeSubscriber.prototype.closeContext = function (context) {
  129. this.destination.next(context.buffer);
  130. var contexts = this.contexts;
  131. var spliceIndex = contexts ? contexts.indexOf(context) : -1;
  132. if (spliceIndex >= 0) {
  133. contexts.splice(contexts.indexOf(context), 1);
  134. }
  135. };
  136. return BufferTimeSubscriber;
  137. }(Subscriber_1.Subscriber));
  138. function dispatchBufferTimeSpanOnly(state) {
  139. var subscriber = state.subscriber;
  140. var prevContext = state.context;
  141. if (prevContext) {
  142. subscriber.closeContext(prevContext);
  143. }
  144. if (!subscriber.closed) {
  145. state.context = subscriber.openContext();
  146. state.context.closeAction = this.schedule(state, state.bufferTimeSpan);
  147. }
  148. }
  149. function dispatchBufferCreation(state) {
  150. var bufferCreationInterval = state.bufferCreationInterval, bufferTimeSpan = state.bufferTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler;
  151. var context = subscriber.openContext();
  152. var action = this;
  153. if (!subscriber.closed) {
  154. subscriber.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber: subscriber, context: context }));
  155. action.schedule(state, bufferCreationInterval);
  156. }
  157. }
  158. function dispatchBufferClose(arg) {
  159. var subscriber = arg.subscriber, context = arg.context;
  160. subscriber.closeContext(context);
  161. }
  162. //# sourceMappingURL=bufferTime.js.map