Skip to content

Subject

DavidMGross edited this page Jul 18, 2013 · 22 revisions

A Subject is a sort of bridge or proxy that acts both as an Observer and as an Observable. Because it is an Observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.

There are four subclasses of Subject that are designed for particular use cases:

  • AsyncSubject — emits the last value (and only the last value) emitted by the source Observable(s), and only after that source Observable(s) completes
  • BehaviorSubject — when an Observer subscribes, begins by emitting the item most recently emitted by the source Observable and then continues to emit any other items emitted later by the source Observable(s)
  • PublishSubject — emits to a subscriber only those items that are emitted by the source Observable(s) subsequent to the time of the subscription
  • ReplaySubject — emits to any subscriber all of the items that were emitted by the source Observable(s), regardless of when the subscriber subscribes
Clone this wiki locally