Why RXJS angular behaviorSubject emit mutiple values

允我心安 提交于 2020-07-22 06:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!