In Angular service i have created the subject and returning it as observable as follows :
public entityPayLoad = new Subject<Entity>();
public getSubjectAsObservable() {
return this.entityPayLoad .asObservable();
}
I am calling next method of subject from two different components as :
From Comp 1 :
this.Service.entityPayLoad.next(this.entity1);
From Comp 2 :
this.Service.entityPayLoad.next(this.entity2);
I am subscribing to subject as follows which is getting called twice for one next method call:
this.entitiesSub = this.Service.getSubjectAsObservable().subscribe((entity) => {
this.loadNewMainEntity(selectedMainEntity);
});
Initially it works fine, but after getting two next method call, then for every subsequent one next request, subscriber gets called twice.
I have unsubscribed the same in ngondestroy. But as i am not leaving the component for subsequent next call, unsubscribe does not get called.