ReplaySubject.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /** PURE_IMPORTS_START tslib,_Subject,_scheduler_queue,_Subscription,_operators_observeOn,_util_ObjectUnsubscribedError,_SubjectSubscription PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subject } from './Subject';
  4. import { queue } from './scheduler/queue';
  5. import { Subscription } from './Subscription';
  6. import { ObserveOnSubscriber } from './operators/observeOn';
  7. import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
  8. import { SubjectSubscription } from './SubjectSubscription';
  9. var ReplaySubject = /*@__PURE__*/ (function (_super) {
  10. tslib_1.__extends(ReplaySubject, _super);
  11. function ReplaySubject(bufferSize, windowTime, scheduler) {
  12. if (bufferSize === void 0) {
  13. bufferSize = Number.POSITIVE_INFINITY;
  14. }
  15. if (windowTime === void 0) {
  16. windowTime = Number.POSITIVE_INFINITY;
  17. }
  18. var _this = _super.call(this) || this;
  19. _this.scheduler = scheduler;
  20. _this._events = [];
  21. _this._infiniteTimeWindow = false;
  22. _this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
  23. _this._windowTime = windowTime < 1 ? 1 : windowTime;
  24. if (windowTime === Number.POSITIVE_INFINITY) {
  25. _this._infiniteTimeWindow = true;
  26. _this.next = _this.nextInfiniteTimeWindow;
  27. }
  28. else {
  29. _this.next = _this.nextTimeWindow;
  30. }
  31. return _this;
  32. }
  33. ReplaySubject.prototype.nextInfiniteTimeWindow = function (value) {
  34. if (!this.isStopped) {
  35. var _events = this._events;
  36. _events.push(value);
  37. if (_events.length > this._bufferSize) {
  38. _events.shift();
  39. }
  40. }
  41. _super.prototype.next.call(this, value);
  42. };
  43. ReplaySubject.prototype.nextTimeWindow = function (value) {
  44. if (!this.isStopped) {
  45. this._events.push(new ReplayEvent(this._getNow(), value));
  46. this._trimBufferThenGetEvents();
  47. }
  48. _super.prototype.next.call(this, value);
  49. };
  50. ReplaySubject.prototype._subscribe = function (subscriber) {
  51. var _infiniteTimeWindow = this._infiniteTimeWindow;
  52. var _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
  53. var scheduler = this.scheduler;
  54. var len = _events.length;
  55. var subscription;
  56. if (this.closed) {
  57. throw new ObjectUnsubscribedError();
  58. }
  59. else if (this.isStopped || this.hasError) {
  60. subscription = Subscription.EMPTY;
  61. }
  62. else {
  63. this.observers.push(subscriber);
  64. subscription = new SubjectSubscription(this, subscriber);
  65. }
  66. if (scheduler) {
  67. subscriber.add(subscriber = new ObserveOnSubscriber(subscriber, scheduler));
  68. }
  69. if (_infiniteTimeWindow) {
  70. for (var i = 0; i < len && !subscriber.closed; i++) {
  71. subscriber.next(_events[i]);
  72. }
  73. }
  74. else {
  75. for (var i = 0; i < len && !subscriber.closed; i++) {
  76. subscriber.next(_events[i].value);
  77. }
  78. }
  79. if (this.hasError) {
  80. subscriber.error(this.thrownError);
  81. }
  82. else if (this.isStopped) {
  83. subscriber.complete();
  84. }
  85. return subscription;
  86. };
  87. ReplaySubject.prototype._getNow = function () {
  88. return (this.scheduler || queue).now();
  89. };
  90. ReplaySubject.prototype._trimBufferThenGetEvents = function () {
  91. var now = this._getNow();
  92. var _bufferSize = this._bufferSize;
  93. var _windowTime = this._windowTime;
  94. var _events = this._events;
  95. var eventsCount = _events.length;
  96. var spliceCount = 0;
  97. while (spliceCount < eventsCount) {
  98. if ((now - _events[spliceCount].time) < _windowTime) {
  99. break;
  100. }
  101. spliceCount++;
  102. }
  103. if (eventsCount > _bufferSize) {
  104. spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
  105. }
  106. if (spliceCount > 0) {
  107. _events.splice(0, spliceCount);
  108. }
  109. return _events;
  110. };
  111. return ReplaySubject;
  112. }(Subject));
  113. export { ReplaySubject };
  114. var ReplayEvent = /*@__PURE__*/ (function () {
  115. function ReplayEvent(time, value) {
  116. this.time = time;
  117. this.value = value;
  118. }
  119. return ReplayEvent;
  120. }());
  121. //# sourceMappingURL=ReplaySubject.js.map