Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • Thanks for your comment. I'll try it out, but I always thought that "takeUntil(this.destroy$)" takes care of unsubscribing. Like you can either unsubscribe or add this takeUntil parameter instead. Note that this event Listener is not part of a component, but a service on its own triggered on app start Commented Jul 5 at 8:45
  • @SergejBjakow sorry I missed the takeUntil part, but what you mentioned might help, instead of subscribing inside the service return the observable in the service method. Then subscribe to the observable in the component, along with this subscription, we can remove take until for this particular scenario, my understanding is this.destroyed$ is not reset when component is destroyed, mostly keep the subscriptions at the component level, the service returns observables for the component to subscribe Commented Jul 5 at 8:47
  • Note that the entire logic is part of a Service (provided in root) and NOT a component private destroy$ = new Subject(); ngOnDestroy(): void { this.destroy$.next(null); } I'm not sure if that makes even sense within a service, so maybe there is even more to refactor... Commented Jul 5 at 9:57
  • @SergejBjakow The service if its providedIn: 'root' is never destroyed until you refresh the page. If the service is added to the providers array of a component or module. it will be destroyed only when the parent component/component is destroyed. So keep this mind and check if the service is destroyed when the component is destroyed, so this.destroy$.next(null) not being called is the problem. Commented Jul 5 at 10:00
  • 1
    @SergejBjakow When you are to providers array, ngOnDestroy is called. Try this approach from now onwards. This will give you clean code Commented Jul 5 at 13:51