I have a component that is present on my application in two differente place. This component has a dependency, one service, that fetch your data by http call.
I don't want that this service does two http call, but only one.
This is my component:
export class MenuFrontComponent {
menu: any;
constructor(private router: Router, public l: LanguageService, private _menu: MenuService) {
this._menu.getMenu().subscribe((menu) => {
this.menu = menu;
});
}
This is my service:
constructor(private client: ClientService, public l: LanguageService) {
this.client.getMenu(this.l.getLang).subscribe(data => {
this.menu = data;
});
}
getMenu(): any {
return new Observable(observer => {
let int = setInterval(()=>{
if(this.menu){
observer.next(this.menu);
observer.complete();
clearInterval(int);
}
}, 200);
});
}
I'm using a setInterval to avoid that this call is done two times, because my component, present two times, calls at the same time, getMenu by MenuService.
I hope to explain well.