In my angular application I have a service that loads data from a server.
load.service.ts:
load = new Observable(observer => {
console.log('load function called');
// async stuff that takes time
observer.complete();
});
I also have a bunch of components that all need to wait until the load observable completes. I do this like this:
this.loadService.load.subscribe(
change => {},
error => {},
() => {
// do stuff with data
}
);
However, if I have multiple components all subscribing, the load function gets called multiple times, sending lots of requests to the server (as seen by console.log('load function called');.
My Question
I know there are ways to get around this using Angular, but my question is this:
How do I have multiple subscriptions to one observable but only call the function once?
My question is not about caching data. It is about observables.
loadfunction as an example of how my question applies. I just want to know if it is possible to subscribe to a function without calling it multiple times