0
votes

I have a hot RxJS Observable that I want to respond to in different ways depending on the context of the application. The Subject emits a new event based on some global action intercepted by a directive, but then I want

  • If a child component is subscribing to the Subject, then the child should handle the event
  • Otherwise, use a global handler

I can get the number of subscribers from the Subject and then tell the global handler to ignore if there are multiple subscribers, but it's not part of the API, so it seems like it may not be the right way to handle it. So what is the right way to do this?

Also, should the global event handler be part of the directive, the service, or should that be in a new component?

1
The global handler is an observer itself?martin
@martin The global handler can subscribe to the Subject. It would have to in order to know that an event has occurred I think.Keith

1 Answers

0
votes

You can put global event subject in a global app.service and inject it in other component for subscription.

Although the ideal component should just have its own service maybe to handle complex event, but sometimes I feel directly inject a global service keeps the code cleaner. Otherwise, if you really want complete isolation or the component should be widely reusable e.g UI dropdown list, I suggest to use @Output to fire events (btw angular EventEmitter inherit Subject) and @Input to take in variables.