I have one stupid question maybe: I'm implementing some routes guards that contains some other observable subscription, they are provided in root, so they should be singleton, no need to unsubscribe really. But what I'm doing is returning all the time a new Observable that I completes manually (not sure if this is required), the internal subscription are cleared each execution with the private _sub field.
Question is: is this code safe from memory leaks (how is routing guard subscribed internally to this observable)? there is a recommended way to test and see if there are observables not unsubscribed in one application ?
@Injectable({
providedIn: 'root'
})
export class MyRedirectGuard implements CanActivate {
private _sub: Subscription;
constructor(private myService: MyService,
private router: Router) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> {
if (this._sub)
this._sub.unsubscribe();
return new Observable<boolean | UrlTree>((observer: any) => {
this._sub = this.myService.serviceReady$.subscribe({
next: ready => {
//Navigate to Overview or to specific machine if single station
if (ready) {
//some other code to redirect
//if (condition) this.router.navigate(['mainPage'])
//else this.router.navigate(['secondPage'])
observer.next(false);
observer.complete();
}
}
}
);
});
}
}