What is the difference between Subject and BehaviorSubject?

后端 未结 5 832
小蘑菇
小蘑菇 2020-11-22 04:53

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

相关标签:
5条回答
  • 2020-11-22 04:54

    I just created a project which explain what is the difference between all subjects:
    https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async

    0 讨论(0)
  • 2020-11-22 05:01

    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
    
    0 讨论(0)
  • 2020-11-22 05:02

    BehaviorSubject keeps in memory the last value that was emitted by the observable. A regular Subject doesn't.

    BehaviorSubject is like ReplaySubject with a buffer size of 1.

    0 讨论(0)
  • 2020-11-22 05:03

    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
    0 讨论(0)
  • 2020-11-22 05:11

    It might help you to understand.

    import * as Rx from 'rxjs';
    
    const subject1 = new Rx.Subject();
    subject1.next(1);
    subject1.subscribe(x => console.log(x)); // will print nothing -> because we subscribed after the emission and it does not hold the value.
    
    const subject2 = new Rx.Subject();
    subject2.subscribe(x => console.log(x)); // print 1 -> because the emission happend after the subscription.
    subject2.next(1);
    
    const behavSubject1 = new Rx.BehaviorSubject(1);
    behavSubject1.next(2);
    behavSubject1.subscribe(x => console.log(x)); // print 2 -> because it holds the value.
    
    const behavSubject2 = new Rx.BehaviorSubject(1);
    behavSubject2.subscribe(x => console.log('val:', x)); // print 1 -> default value
    behavSubject2.next(2) // just because of next emission will print 2 
    
    0 讨论(0)
提交回复
热议问题