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<any>;
constructor(private http: HttpClient) {}
getSomeData(): Observable<any> {
if (this.someDataObservable) {
return this.someDataObservable;
} else {
this.someDataObservable = this.http.get<any>('some/endpoint').pipe(share());
return this.someDataObservable;
}
}
}
I am still open to more efficient suggestions!
For the curious: share()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…