I want to know, when to unsubscribe on a Observable Subscription. And when there is no need to unsubscribe. It is kind of hard to understand the concept of the subscriptions.
I give you an example:
this.http
.get(
environment.baseUrl + "/user/init",
this.storageService.getAuthHeader(),
)
.subscribe(
(user: AppUser) => {
console.log(user.username);
this.currentUser = user;
},
(err: HttpErrorResponse) => {
log(err);
this.router.navigateByUrl("/login");
},
)
.add((teardown: TeardownLogic) => {
// this will get called always after the User is retrieved/ subscription is unsubscribed
console.log("Unsubscribe automatically ?");
});
in this call I get an Observable. But i get told that I need to manually "unsubscribe" every subscription, after a Component is destroyed. But no one can clearly explain why.
Also I always get the recommendation to use Pipes (which makes sense I think, when dealing with multiple asynchronous calls which are dependent on each other), but I wonder when to use it, because the whole pipe().take(1) ... call seems hacky or like a workaround "
I also read the documentation about Subscriptions RxJS which doesn't help me that much, but to know, that "teardownLogic" will always be applied after .unsubscribe()
I also read Difference between the methods .pipe() and .subscribe() on a RXJS observable
and
Angular/RxJs When should I unsubscribe from `Subscription`
which both don't really answer the "mechanics" of Subscription in RxJS / Angular