Currently have a scenario where a method within a shared service is used by multiple components. This method makes an HTTP call to an endpoint that will always have the same
After trying a few different methods, came across this one that resolves my issue and only makes one HTTP request no matter how many subscribers:
class SharedService {
someDataObservable: Observable;
constructor(private http: HttpClient) {}
getSomeData(): Observable {
if (this.someDataObservable) {
return this.someDataObservable;
} else {
this.someDataObservable = this.http.get('some/endpoint').pipe(share());
return this.someDataObservable;
}
}
}
I am still open to more efficient suggestions!
For the curious: share()