Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Subject and BehaviorSubject?

I'm not clear on the difference between a Subject and a BehaviorSubject. Is it just that a BehaviorSubject has the getValue() function?

like image 825
Mike Jerred Avatar asked Apr 11 '17 14:04

Mike Jerred


People also ask

What is difference between BehaviorSubject and observable?

Observable is a Generic, and BehaviorSubject is technically a sub-type of Observable because BehaviorSubject is an observable with specific qualities. An observable can be created from both Subject and BehaviorSubject using subject.

What is the use of subject and BehaviorSubject in Angular?

1. A BehaviorSubject behaves like a Subject but it has an extra feature, i.e. it preserves the last emitted value. That means if a value was emitted earlier and if a subscription was added after the value was emitted, then the subscription will give the last value that was emitted.

What are different between of subject BehaviorSubject and ReplaySubject?

The ReplaySubject is comparable to the BehaviorSubject in the way that it can send “old” values to new subscribers. It however has the extra characteristic that it can record a part of the observable execution and therefore store multiple old values and “replay” them to new subscribers.

What is the difference between observable and subject?

While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. Every Subject is an Observable.


2 Answers

A BehaviorSubject holds one value. When it is subscribed it emits the value immediately. A Subject doesn't hold a value.

Subject example (with RxJS 5 API):

const subject = new Rx.Subject(); subject.next(1); subject.subscribe(x => console.log(x)); 

Console output will be empty

BehaviorSubject example:

const subject = new Rx.BehaviorSubject(0); subject.next(1); subject.subscribe(x => console.log(x)); 

Console output: 1

In addition:

  • BehaviorSubject should be created with an initial value: new Rx.BehaviorSubject(1)
  • Consider ReplaySubject if you want the subject to hold more than one value
like image 157
ZahiC Avatar answered Sep 30 '22 14:09

ZahiC


BehaviourSubject

BehaviourSubject will return the initial value or the current value on Subscription

var bSubject= new Rx.BehaviorSubject(0);  // 0 is the initial value  bSubject.subscribe({   next: (v) => console.log('observerA: ' + v)  // output initial value, then new values on `next` triggers });  bSubject.next(1);  // output new value 1 for 'observer A' bSubject.next(2);  // output new value 2 for 'observer A', current value 2 for 'Observer B' on subscription  bSubject.subscribe({   next: (v) => console.log('observerB: ' + v)  // output current value 2, then new values on `next` triggers });  bSubject.next(3); 

With output:

observerA: 0 observerA: 1 observerA: 2 observerB: 2 observerA: 3 observerB: 3 

Subject

Subject does not return the current value on Subscription. It triggers only on .next(value) call and return/output the value

var subject = new Rx.Subject();  subject.next(1); //Subjects will not output this value  subject.subscribe({   next: (v) => console.log('observerA: ' + v) }); subject.subscribe({   next: (v) => console.log('observerB: ' + v) });  subject.next(2); subject.next(3); 

With the following output on the console:

observerA: 2 observerB: 2 observerA: 3 observerB: 3 
like image 38
Mohammed Safeer Avatar answered Sep 30 '22 15:09

Mohammed Safeer