I have the following problem, I have several controls that subscribe to an http service that returns the data of a project according to the project code, calls of this type:
this.proyectosService.getByCodigo('xxx').subscribe(response => {
// hacer algo
});
the service method is nothing special:
public getByCodigo(codigo: string) {
return this.http.get(this.url + codigo).map(response => response.json());
}
The issue is that I'm seeing that too many requests are made in a row, to the same url so to optimize times it occurs to me that I could save the last returned object in the memory of the service and check with the server to check if the requested object is the one that I have in memory (compare the project code), the problem is that I do not know how to return in case it already has in memory the object as an observable so that it is transparent for the controls that subscribe to the service , the idea is to do something like this:
public getByCodigo(codigo: string) {
if (this.ultimoProyecto.codigo === codigo) {
// aqui el problema como devuelvo los datos
} else {
return this.http.get(this.url + codigo).map(response => {
this.ultimoProyecto = response.json() // actualizo referencia al ultimo devuelto
return response.json();
});
}
}