There is a config data in project which is used by multiple components and services. The idea is to build a service which gets just once the data from API and provides it cached every time is needed.
private getConfig(): Observable<InitConfig> {
return this.http.get('public/config/initconfig')
.pipe(
tap((config: InitConfig) => {
this.config = config; // set the config
}),
);
}
public getInitConfig(): Observable<InitConfig> {
if ( _.isEmpty(this.config) ) {
return this.getConfig();
} else {
return of(this.config);
}
}
The problem is when the get request is on progress and the data is not cached yet. If another consumer calls getInitConfig() at this time I get duplicated get request.