I am passing a subject as an observable from a service to a resolver. The service may complete the observable when an http request completes or when it finds that the data it needs is cached. In the second case, where the data is cached it looks like the observable completes too early because the route transition does not happen. if i put the requestSubject.complete() inside a setTimeout with some timeout it works but otherwise it doesnt.
//resolve function from resolver
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
const id = +route.paramMap.get('id');
const language = route.queryParamMap.get('language');
const request = this.service.get(id, language);
request.pipe(share()).subscribe(
() => {},
() => { this.router.navigateByUrl(AppRoutingConfig.search); });
return request;
}
//the service get function
get(id: number, language: string): Observable<any> {
const requestSubject = new Subject();
if (!isDataCached(id)) {
const url = `some url`;
const sub = this.http.get(url).subscribe((item: any) => {
requestSubject.next(1);
}, () => {
requestSubject.error(0);
}, () => {
requestSubject.complete();
});
} else {
requestSubject.complete();
}
return requestSubject.asObservable();
}