ReplaySubject.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (!this.isStopped) {
  36. const _events = this._events;
  37. _events.push(value);
  38. // Since this method is invoked in every next() call than the buffer
  39. // can overgrow the max size only by one item
  40. if (_events.length > this._bufferSize) {
  41. _events.shift();
  42. }
  43. }
  44. super.next(value);
  45. }
  46. private nextTimeWindow(value: T): void {
  47. if (!this.isStopped) {
  48. this._events.push(new ReplayEvent(this._getNow(), value));
  49. this._trimBufferThenGetEvents();
  50. }
  51. super.next(value);
  52. }
  53. /** @deprecated This is an internal implementation detail, do not use. */
  54. _subscribe(subscriber: Subscriber<T>): Subscription {
  55. // When `_infiniteTimeWindow === true` then the buffer is already trimmed
  56. const _infiniteTimeWindow = this._infiniteTimeWindow;
  57. const _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
  58. const scheduler = this.scheduler;
  59. const len = _events.length;
  60. let subscription: Subscription;
  61. if (this.closed) {
  62. throw new ObjectUnsubscribedError();
  63. } else if (this.isStopped || this.hasError) {
  64. subscription = Subscription.EMPTY;
  65. } else {
  66. this.observers.push(subscriber);
  67. subscription = new SubjectSubscription(this, subscriber);
  68. }
  69. if (scheduler) {
  70. subscriber.add(subscriber = new ObserveOnSubscriber<T>(subscriber, scheduler));
  71. }
  72. if (_infiniteTimeWindow) {
  73. for (let i = 0; i < len && !subscriber.closed; i++) {
  74. subscriber.next(<T>_events[i]);
  75. }
  76. } else {
  77. for (let i = 0; i < len && !subscriber.closed; i++) {
  78. subscriber.next((<ReplayEvent<T>>_events[i]).value);
  79. }
  80. }
  81. if (this.hasError) {
  82. subscriber.error(this.thrownError);
  83. } else if (this.isStopped) {
  84. subscriber.complete();
  85. }
  86. return subscription;
  87. }
  88. _getNow(): number {
  89. return (this.scheduler || queue).now();
  90. }
  91. private _trimBufferThenGetEvents(): ReplayEvent<T>[] {
  92. const now = this._getNow();
  93. const _bufferSize = this._bufferSize;
  94. const _windowTime = this._windowTime;
  95. const _events = <ReplayEvent<T>[]>this._events;
  96. const eventsCount = _events.length;
  97. let spliceCount = 0;
  98. // Trim events that fall out of the time window.
  99. // Start at the front of the list. Break early once
  100. // we encounter an event that falls within the window.
  101. while (spliceCount < eventsCount) {
  102. if ((now - _events[spliceCount].time) < _windowTime) {
  103. break;
  104. }
  105. spliceCount++;
  106. }
  107. if (eventsCount > _bufferSize) {
  108. spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
  109. }
  110. if (spliceCount > 0) {
  111. _events.splice(0, spliceCount);
  112. }
  113. return _events;
  114. }
  115. }
  116. class ReplayEvent<T> {
  117. constructor(public time: number, public value: T) {
  118. }
  119. }