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.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { Subject } from './Subject';
  2. import { SchedulerLike } from './types';
  3. import { queue } from './scheduler/queue';
  4. import { Subscriber } from './Subscriber';
  5. import { Subscription } from './Subscription';
  6. import { ObserveOnSubscriber } from './operators/observeOn';
  7. import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
  8. import { SubjectSubscription } from './SubjectSubscription';
  9. /**
  10. * A variant of Subject that "replays" or emits old values to new subscribers.
  11. * It buffers a set number of values and will emit those values immediately to
  12. * any new subscribers in addition to emitting new values to existing subscribers.
  13. *
  14. * @class ReplaySubject<T>
  15. */
  16. export class ReplaySubject<T> extends Subject<T> {
  17. private _events: (ReplayEvent<T> | T)[] = [];
  18. private _bufferSize: number;
  19. private _windowTime: number;
  20. private _infiniteTimeWindow: boolean = false;
  21. constructor(bufferSize: number = Number.POSITIVE_INFINITY,
  22. windowTime: number = Number.POSITIVE_INFINITY,
  23. private scheduler?: SchedulerLike) {
  24. super();
  25. this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
  26. this._windowTime = windowTime < 1 ? 1 : windowTime;
  27. if (windowTime === Number.POSITIVE_INFINITY) {
  28. this._infiniteTimeWindow = true;
  29. this.next = this.nextInfiniteTimeWindow;
  30. } else {
  31. this.next = this.nextTimeWindow;
  32. }
  33. }
  34. private nextInfiniteTimeWindow(value: T): void {
  35. const _events = this._events;
  36. _events.push(value);
  37. // Since this method is invoked in every next() call than the buffer
  38. // can overgrow the max size only by one item
  39. if (_events.length > this._bufferSize) {
  40. _events.shift();
  41. }
  42. super.next(value);
  43. }
  44. private nextTimeWindow(value: T): void {
  45. this._events.push(new ReplayEvent(this._getNow(), value));
  46. this._trimBufferThenGetEvents();
  47. super.next(value);
  48. }
  49. /** @deprecated This is an internal implementation detail, do not use. */
  50. _subscribe(subscriber: Subscriber<T>): Subscription {
  51. // When `_infiniteTimeWindow === true` then the buffer is already trimmed
  52. const _infiniteTimeWindow = this._infiniteTimeWindow;
  53. const _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
  54. const scheduler = this.scheduler;
  55. const len = _events.length;
  56. let subscription: Subscription;
  57. if (this.closed) {
  58. throw new ObjectUnsubscribedError();
  59. } else if (this.isStopped || this.hasError) {
  60. subscription = Subscription.EMPTY;
  61. } else {
  62. this.observers.push(subscriber);
  63. subscription = new SubjectSubscription(this, subscriber);
  64. }
  65. if (scheduler) {
  66. subscriber.add(subscriber = new ObserveOnSubscriber<T>(subscriber, scheduler));
  67. }
  68. if (_infiniteTimeWindow) {
  69. for (let i = 0; i < len && !subscriber.closed; i++) {
  70. subscriber.next(<T>_events[i]);
  71. }
  72. } else {
  73. for (let i = 0; i < len && !subscriber.closed; i++) {
  74. subscriber.next((<ReplayEvent<T>>_events[i]).value);
  75. }
  76. }
  77. if (this.hasError) {
  78. subscriber.error(this.thrownError);
  79. } else if (this.isStopped) {
  80. subscriber.complete();
  81. }
  82. return subscription;
  83. }
  84. _getNow(): number {
  85. return (this.scheduler || queue).now();
  86. }
  87. private _trimBufferThenGetEvents(): ReplayEvent<T>[] {
  88. const now = this._getNow();
  89. const _bufferSize = this._bufferSize;
  90. const _windowTime = this._windowTime;
  91. const _events = <ReplayEvent<T>[]>this._events;
  92. const eventsCount = _events.length;
  93. let spliceCount = 0;
  94. // Trim events that fall out of the time window.
  95. // Start at the front of the list. Break early once
  96. // we encounter an event that falls within the window.
  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. }
  112. class ReplayEvent<T> {
  113. constructor(public time: number, public value: T) {
  114. }
  115. }