Last few weeks I have been dealing with memory leaks in my angular app. I have been told that each subscription should have an unsubscribe() call somewhere to prevent any corresponding memory leaks. Thus, I have been adding the following code through my project:
this.testSubscription?.unsubscribe();
this.testSubscription = {observable}.subscribe();
However, I have found out, that in case my component gets destroyed and there is an ongoing backend request it gets automatically canceled, and thus the unexpected result is shown to the user.
Why I am using such a concept?
- I have a list of items where I am able to delete them. The deletion could potentially take some time and thus I have implemented an optimistic response, sending BE request and deleting the inner component from the list.
Do you think that having a subscription variable is in this case unnecessary and could be removed or do you suggest any other approach to this problem that won't introduce any new memory leaks?
Thanks for your help!