Situation: Within my Angular application, I am using publishReplay to store and replay specific Http requests. However, I encountered an issue where I need the cached observable source to update itself and create a new cached observable with publishReplay whenever a URL parameter change is detected.
I have been unable to compare the newly proposed observable with the existing cached observable's source effectively in order to achieve this desired functionality.
Seeking Solution: I am looking for a way to compare two observables (specifically their URLs) without subscribing to them. This comparison will help me determine if the parameters on the URL have changed so that I can decide whether to refresh the cache or return the cached result.
Here is an example of what I have tried unsuccessfully so far:
protected isEntreeValid<T>(key: ICacheKeyConstantsModel, source: Observable<T>): boolean {
if (!this.has(key)) { return false; }
if (this.has(key)) {
let origSource = this.cache.registry.get(key).source;
console.log('source: ', source);
console.log('are they equal', source.source === origSource.source);
if (source.source !== origSource.source) { return false; }
}
return true;
}
Explanation: The 'source' parameter in the above method points to an http.get function stored in a Map. It is crucial to evaluate this source when a key already exists in order to check for any URL parameter changes and handle the cache accordingly.
Note: My main focus is on comparing the source properties of the Observables and not connecting to them or analyzing the streams they emit.
Update: I discovered the location of the property needed for comparison down the structure of the Observables. Is there a better way to access this information instead of drilling down so deeply?
Property Location: Observable.value.source.source.source.source.source
Structure at Location:
{
value: HttpRequest
body: null
headers: HttpHeaders
method: "GET"
params: HttpParams
reportProgress: false
responseType: "json"
url: "https://my-dev-server.com/primary/HealthCareApp/Patients"
urlWithParams: "https://my-dev-server.com/primary/HealthCareApp/Patients"
Question: Are there efficient ways to flatten or drill-down to the innermost Observable without subscribing?
Update 2:
Currently, I am using a method like the one below to extract the innerObservable, but I am open to exploring better alternatives:
private extractInnerObservable<T>(source: Observable<T>): Observable<T> {
let target = observableOf(source)['value'];
while (target['source']) {
target = target['source'];
}
return target;
}
Final Notes:
While I continue to explore other options, I wanted to provide updates on my progress with addressing the initial problem. Although the current solution may not be the most elegant, it has helped me make some headway. Hopefully, sharing this process will assist others facing similar challenges.