BehaviorSubject is executed more than once

behaviorsubject wait for value
behaviorsubject get last value
behaviorsubject subscribe called twice
rxjs subject
behaviorsubject subscribe only once
subject in javascript

I am using angular 4 for my app and I use behaviorSubject to communicate between component, everythings were working fine

Here is my code:

export class Globals {
loadStudentsByClass: BehaviorSubject<string> = new BehaviorSubject<string>(null);
}

export class ClassComponent implements OnInit {
selectNewClass() {
    console.log('About to select new class', this.selectedClass);
    this.globals.loadStudentsByClass.next(this.selectedClass); 
    this.globals.changeClassBehavior.next(true);
    this.router.navigateByUrl('');
  }
}

export class StudentComponent implements OnInit {
    this.globals.loadStudentsByClass.subscribe(value => {
          if (!isNullOrUndefined(value) && this.selectedClass !== value) {
            console.log('loadStudentsByClass=> home');
            this.loadStudentsByClass(this.selectedClass);
          }
        });
}

So, I have the component ClassComponent who triggers the StudentComponent to call the function loadStudentsByClass.

This is the only place where I call this behavior subject, but I find my behavior Subject emitted more than once

The problem is why behaviorSubject execute loadStudentsByClass more than once although this.globals.loadStudentsByClass.next(this.selectedClass); is called only one time

By the way the number of execution depends on the numbers of views I visited in the application


You probably forgot to unsubscribe in your StudentComponent when the components gets destroyed. Save the subscription in a class variable and in ngOnDestroy() lifecycle hook call subscription.unsubscribe()

BehaviorSubject is executed more than once - angular - html, This is the only place where I call this behavior subject, but I find my behavior Subject emitted more than once The problem is why behaviorSubject execute� BehaviorSubject is executed more than once. 1. Best way to read and update a value of a BehaviorSubject. 0. BehaviorSubject subscriber gets same next() element


This will call twice, because whenever the behaviourSubject changes it will emit the latest values. Previously it was null (as default value) which you have given it and next when the service response came you are doing next so it will emit the latest value after change, which is fair enough. For your code you should only check for

if(value){// do the stuff here}

Understanding rxjs BehaviorSubject, ReplaySubject and , One of the variants of the Subject is the BehaviorSubject. the observable execution and therefore store multiple old values and “replay” them to 2 values, but no longer than a 100 ms; We start subscribing with Subscriber A� BehaviorSubject is executed more than once; Content is set more than once “ERROR: column … specified more than once” in VIEW definition;


Try a replay subject that has no initial value but will still cache your stream:

export class Globals {
loadStudentsByClass = new ReplaySubject<string>(1);
}

Inconsistent execution order when calling BehaviorSubject.next , Inconsistent execution order when calling BehaviorSubject.next inside the to subscription execution happens immediately for first , then observer is can use any rxjs magic to dispatch one or more new actions to the store. Currently I am working in a rather large organization quite a few teams and projects (more than 40 SPAs) that are in the process of migration to Angular and therefore also RxJs. This represents a great opportunity to get in touch with the confusing parts of RxJs which can be easy to forget once one masters the APIs and focuses on the


Why does a sequence get called multiple times when there are , In my mind I thought this worked more closely to streams and piping. Then when I call process.subscribe I would be subscribing to the maps output, not only be called once when I subscribe multiple times to the map observable. Mutation could be avoided without re running the map function by treating� Once we have our special zone created, we run the same program inside of myZone.run. This is essentially what Angular 2 does to create its custom NgZone zone and set up change detection (which we will talk more about in just a moment). So, any time that a task is executed within the context of a zone, we can detect when it starts and when it


How to use subjects to multicast observers in RxJS, which allows you to multicast values to more than one observer. It acts exactly the same as the behavior subject but can only execute after� BehaviorSubject. One of the variants of Subjects is the BehaviorSubject, which has a notion of "the current value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value" from the BehaviorSubject. BehaviorSubjects are useful for representing "values over time".


Using BehaviorSubject for Values That Change over Time, Get exclusive content, resources, and more! Subscribe. 1-2 emails per week, no spam. We you! Help spread the word about this tutorial! You need to be a Pro� These are objects which can emit one or more data packets. Other objects can subscribe to these Observables and run a callback each time data is emitted. (In this example using the Http service, each Observable will only emit data once, but a different type of Observable could emit data more than once.)