Cross Package Events


#1

Now that I have Rest services working with Request and I have a Service that I am leveraging to communicate between packages I have another issue. I have a promise in one of my packages that sets a variable as soon as the rest response resolves. I would like to have it emit an event that the other packages can subscribe to. However all of the event implementations {Event-Kit} I have seen only seem to apply to within a package or a package / view.


#2

I would highly recommend using event-kit. There’s nothing particularly special about the separation of packages. You just need to be able to get a reference to the object that will raise the event and then subscribe to it. It’s just that it is hard to get a reference to an object that was created by some other package if they don’t provide a service. But with getting the service up and running, you’ve already implemented something that solves that problem.

If you look at Atom, everything in it follows this pattern. For example, to be notified that a new editor was opened, I can use the observeTextEditors subscription method on the Workspace class. It’s just that it is a lot easier to get to the Workspace instance because it’s globally accessible at atom.workspace.

To mimic this, you could add something to the service object you give to consumers. This something could raise events that consumers could monitor and/or allow consumers to raise pre-specified events for other packages that consume the service to subscribe to. Or the service could be a registry of sorts along the lines of the ViewRegistry in Atom.

There’s lots of possibilities.