There are tons of info out there on using 'takeUntil' operator to unsubscribe from a stream, like so:
...
export class CategoryOptionInputComponent
constructor(private svc: MyService, protected router: RouterService) {
...
this.router.routerUrl$.pipe(takeUntil(this.ngUnsubscribe)).subscribe(s => {
const param1 = s.state.featureId;
this.svc.InitFromApi(param1);
});
...
}
ngOnDestroy() {
this.ngUnsubscribe.next();//<-- This line is confusing
this.ngUnsubscribe.complete();
}
private readonly ngUnsubscribe = new Subject();
}
As the above code shown, we needed to load data from API on router data being parsed, and continue to load new data when new router data arrives, until the component is destroyed.
When a user navigating to a different route, the component is expected to be destroyed without a last hit on the API. However it does not behave as expected. The call to API is made even if the component is about to be destroyed.
My questions are:
(1) How to prevent the component to hit the API before it dies?
(2) Why not just complete() the stream, why a next() call before complete() (see the above code)?
Thanks ahead.