На вашем месте я бы сделал:
// service
import { BehaviorSubject } from 'rxjs';
....
public customerCache$: BehaviorSubject<any> = new BehaviorSubject(null);
getCustomerOverview(id) {
return this.apiClient.getCustomer(id, '');
}
.....
// component
import { of } from 'rxjs;
import { switchMap, take } from 'rxjs/operators';
.....
this.customerService.customerCache$.pipe(
// take(1) to kill the subscription after the subscribe, I am scared of an infinite loop because of the .next in the subscribe
take(1),
switchMap(cache => {
// if the cache is truthy, great, use it
if (cache) {
return of(cache);
} else {
// else make an API call
return this.customerService.getCustomerOverview(this.id);
}
}),
).subscribe(x => {
// store x as the cache
this.customerService.customerCache$.next(x);
// make sure this console doesn't log infinitely
console.log(x);
this.getResultValues(x);
});
Нет необходимости отписываться от http
вызовов в Angular, поскольку они конечны.
Теперь везде вам нужно прочитать значение из, вы можете прочитать его из кэша, аналогично этому. Я не фанат этого, потому что я бы использовал Ngrx. Продолжая этот путь, вы создаете код спагетти.