Skip to main content
11 events
when toggle format what by license comment
Jul 6 at 7:46 comment added Naren Murali @SergejBjakow yes exactly!
Jul 5 at 15:05 comment added Sergej Bjakow My takeaway now is that in general it is not a wise idea to use a root service for listening to any kind of event. Event listening should be defined within component scopes, to have more control over such issues
Jul 5 at 15:03 vote accept Sergej Bjakow
Jul 5 at 13:51 comment added Naren Murali @SergejBjakow When you are to providers array, ngOnDestroy is called. Try this approach from now onwards. This will give you clean code
Jul 5 at 13:48 comment added Sergej Bjakow no the service is provided in root. So you are right, the OnDestroy i guess is never triggered. Why is it even allowed to implement the OnDestroy Interface for such Services? Does it make sense at all? To me this sounds like I need to refactor this on a larger scale and associate those event listeners with components...
Jul 5 at 10:00 comment added Naren Murali @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.
Jul 5 at 9:57 comment added Sergej Bjakow 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...
Jul 5 at 9:36 history edited Naren Murali CC BY-SA 4.0
deleted 35 characters in body
Jul 5 at 8:47 comment added Naren Murali @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
Jul 5 at 8:45 comment added Sergej Bjakow 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
Jul 5 at 8:30 history answered Naren Murali CC BY-SA 4.0