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.

ReplaySubject.js 3.5KB

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