ReplaySubject

class stable

A variant of Subject that "replays" old values to new subscribers by emitting them when they first subscribe.

class ReplaySubject<T> extends Subject {
  constructor(_bufferSize: number = Infinity, _windowTime: number = Infinity, _timestampProvider: TimestampProvider = dateTimestampProvider)
  next(value: T): void

  // inherited from index/Subject
  static create: (...args: any[]) => any
  constructor()
  closed: false
  observers: Observer<T>[]
  isStopped: false
  hasError: false
  thrownError: any
  get observed
  lift<R>(operator: Operator<T, R>): Observable<R>
  next(value: T)
  error(err: any)
  complete()
  unsubscribe()
  asObservable(): Observable<T>

  // inherited from index/Observable
  static create: (...args: any[]) => any
  constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic)
  source: Observable<any> | undefined
  operator: Operator<any, T> | undefined
  lift<R>(operator?: Operator<T, R>): Observable<R>
  subscribe(observerOrNext?: Partial<Observer<T>> | ((value: T) => void), error?: (error: any) => void, complete?: () => void): Subscription
  forEach(next: (value: T) => void, promiseCtor?: PromiseConstructorLike): Promise<void>
  pipe(...operations: OperatorFunction<any, any>[]): Observable<any>
  toPromise(promiseCtor?: PromiseConstructorLike): Promise<T | undefined>
}

Description

ReplaySubject has an internal buffer that will store a specified number of values that it has observed. Like Subject, ReplaySubject "observes" values by having them passed to its next method. When it observes a value, it will store that value for a time determined by the configuration of the ReplaySubject, as passed to its constructor.

When a new subscriber subscribes to the ReplaySubject instance, it will synchronously emit all values in its buffer in a First-In-First-Out (FIFO) manner. The ReplaySubject will also complete, if it has observed completion; and it will error if it has observed an error.

There are two main configuration items to be concerned with:

  1. bufferSize - This will determine how many items are stored in the buffer, defaults to infinite.
  2. windowTime - The amount of time to hold a value in the buffer before removing it from the buffer.

Both configurations may exist simultaneously. So if you would like to buffer a maximum of 3 values, as long as the values are less than 2 seconds old, you could do so with a new ReplaySubject(3, 2000).

Differences with BehaviorSubject

BehaviorSubject is similar to new ReplaySubject(1), with a couple fo exceptions:

  1. BehaviorSubject comes "primed" with a single value upon construction.
  2. ReplaySubject will replay values, even after observing an error, where BehaviorSubject will not.

Constructor

constructor(_bufferSize: number = Infinity, _windowTime: number = Infinity, _timestampProvider: TimestampProvider = dateTimestampProvider)

Parameters

_bufferSize

Optional. Default is Infinity.

Type: number.

_windowTime

Optional. Default is Infinity.

Type: number.

_timestampProvider

Optional. Default is dateTimestampProvider.

Type: TimestampProvider.

Methods

next(value: T): void

Parameters

value

Type: T.

Returns

void

See Also

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/class/ReplaySubject