I'm trying to cache requests which we get from multiple objects which can have the same url as array something like this
const a = { urls: ['foo', 'bar'] };
const b = { urls: ['bar', 'other'] };
I don't want to make a new request once I'm iterating through b urls if a url bar has been requested
I've tried this method which is not working for obvious reasons that logic is not complete
private allUrls = [];
private allUrls$: Observable<{ "...someproperties"; url: string }> =
new Observable();
private api$ = this.allUrls$.pipe(
mergeMap(() => this.allUrls$),
shareReplay(1)
);
fetchRequest(urls: string[]) {
urls.map((url) => {
if (!this.allUrls.includes(url)) {
this.allUrls$.pipe(mergeMap(() => this.cacheUrl(url)));
}
});
return this.api$.pipe(
map((response) => {
return response.filter((r) => urls.includes(r.url));
})
);
}
If I am not wrong you are searching solution like this. Please check my comments carefully.
Here you can see working stackblitz url. https://stackblitz.com/edit/angular-eallsn?file=src/main.ts