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';
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.
14 * @class ReplaySubject<T>
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;
22 constructor(bufferSize: number = Number.POSITIVE_INFINITY,
23 windowTime: number = Number.POSITIVE_INFINITY,
24 private scheduler?: SchedulerLike) {
26 this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
27 this._windowTime = windowTime < 1 ? 1 : windowTime;
29 if (windowTime === Number.POSITIVE_INFINITY) {
30 this._infiniteTimeWindow = true;
31 this.next = this.nextInfiniteTimeWindow;
33 this.next = this.nextTimeWindow;
37 private nextInfiniteTimeWindow(value: T): void {
38 const _events = this._events;
40 // Since this method is invoked in every next() call than the buffer
41 // can overgrow the max size only by one item
42 if (_events.length > this._bufferSize) {
49 private nextTimeWindow(value: T): void {
50 this._events.push(new ReplayEvent(this._getNow(), value));
51 this._trimBufferThenGetEvents();
56 /** @deprecated This is an internal implementation detail, do not use. */
57 _subscribe(subscriber: Subscriber<T>): Subscription {
58 // When `_infiniteTimeWindow === true` then the buffer is already trimmed
59 const _infiniteTimeWindow = this._infiniteTimeWindow;
60 const _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
61 const scheduler = this.scheduler;
62 const len = _events.length;
63 let subscription: Subscription;
66 throw new ObjectUnsubscribedError();
67 } else if (this.isStopped || this.hasError) {
68 subscription = Subscription.EMPTY;
70 this.observers.push(subscriber);
71 subscription = new SubjectSubscription(this, subscriber);
75 subscriber.add(subscriber = new ObserveOnSubscriber<T>(subscriber, scheduler));
78 if (_infiniteTimeWindow) {
79 for (let i = 0; i < len && !subscriber.closed; i++) {
80 subscriber.next(<T>_events[i]);
83 for (let i = 0; i < len && !subscriber.closed; i++) {
84 subscriber.next((<ReplayEvent<T>>_events[i]).value);
89 subscriber.error(this.thrownError);
90 } else if (this.isStopped) {
91 subscriber.complete();
98 return (this.scheduler || queue).now();
101 private _trimBufferThenGetEvents(): ReplayEvent<T>[] {
102 const now = this._getNow();
103 const _bufferSize = this._bufferSize;
104 const _windowTime = this._windowTime;
105 const _events = <ReplayEvent<T>[]>this._events;
107 const eventsCount = _events.length;
110 // Trim events that fall out of the time window.
111 // Start at the front of the list. Break early once
112 // we encounter an event that falls within the window.
113 while (spliceCount < eventsCount) {
114 if ((now - _events[spliceCount].time) < _windowTime) {
120 if (eventsCount > _bufferSize) {
121 spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
124 if (spliceCount > 0) {
125 _events.splice(0, spliceCount);
133 class ReplayEvent<T> {
134 constructor(public time: number, public value: T) {