RxJS SUBJECT PRIMER and Multicasting Operators

Ref: https://egghead.io/courses/rxjs-subjects-and-multicasting-operators

Slides: https://goo.gl/photos/ZzdTr66vRvefDQrw6

  • Each observable can only have 1 observer. Hence if 2 observers subscrible to same observable, 2 instances of the observable are created, each of which emit their event to their single observer only.
  • The way to have multiple observers listening to the same shared event stream from single observable is to use Subjects. Moreover, Subjects can also replay events that have happened before observers subscribed.
  • So basically a Subject is neither an observable or observer, but a proxy to orchestrate the single Observable multiple Observers use case
  • Hence, Subject can :
    • subscribe to explicit observable using the .subscribe() function of that observale OR,
    • it can create an implicit observable and so when you do Subject.next(10), to emit 10, the Subject internally creates an observable to emit 10
  • When you do Subject.onNext = function() {}, then Subject creates an observer that it adds to it’s observers list and invokes it whenever the Observable emits next value.
  • Behavior Subjects are subjects that keep track of last emitted value and give it to every observer as soon as it subscribes.
  • Replay Subject replays events to late subscribers even if it has completed, while behavior subject can emit last value only if it’s alive.
  • Async Subjects are like promises coz they too emit the last value only after subject completion.
  • ConnectableObservable an abstraction over a raw subject for shorthand use. It has .connect() method which is same as observable.subscribe(subject), which is how we start an observable (by subscribing to it)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s