1
votes

enter image description hereI use BehaviorSubject to share data between my app components, I have a performance issue due to mutiple emission of same value from BehaviorSubject. For instance, I call http to get team Object from backend and store it in a behaviorSubject, many components subscribe to this BehaviorSubject. Each component gets the value from subscription and does a sequence of manipulations on the value. The essence of the problem is that the value is emitted many times and each component does all sequence a few times. My guess is the the BehaviorSubject emits the value as number of subscribers. I couldn't find anything in Google which is very strange to me, what am I missing ?

The number of times the team emit value is different in local compared to deployment. You can see the the printing of "refetch returned value" it is the actual response from http.

1
what's the purpose of your BehaviorSubject ? If your goal is to share data, why don't you simply store the result of the request in a variable ? - Guerric P
Actually I think it's relate to the http delay time. So how can I filter those fake emission without any value. - Adam007
hi Youkoulele, the purpose is to be able to subscribe to this value and update each component according to the last value stored - Adam007
You should provide at least the relevant parts of your code, or even better a stackblitz so we can find what's wrong in your implementation - Guerric P

1 Answers

0
votes

You can partially fix it by only accepting values different than the last one, thus skipping evaluating the same value a few times in a row.

Also, if the data you query can be the same as the previous query, it may be a good idea to add this anyway:

import { distinctUntilChanged } from 'rxjs/operators';

this.myService.myObservable
    .pipe(distinctUntilChanged())
    .subscribe(value => {
        console.log(value);
    })