p:: JavaScript

  • RxJS
  • Reactive Extensions Library for JavaScript

ReplaySubject

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

ReplaySubject has an internal buffer that will store a specified number of values that it has observed. Like SubjectReplaySubject “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.

constructor(_bufferSize: number = Infinity, _windowTime: number = Infinity, _timestampProvider: TimestampProvider = dateTimestampProvider)
  • _bufferSize
    • The size of the buffer to replay on subscription

Differences with BehaviorSubject

BehaviorSubject is similar to new ReplaySubject(1), with a couple of 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.