问题
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:
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);
})
来源:https://stackoverflow.com/questions/51137505/why-rxjs-angular-behaviorsubject-emit-mutiple-values