ender-js/Ender — Issue #87: Inter-module communication
Author: rvagg
Created: 2011-10-16 05:04:51+00:00
State: open

I'm sure you guys have thought about this issue so I'm wondering what you have to say about it. While it's nice to have modules completely independent there are some cases where some kind of connection would be helpful without creating dependencies. One example I'm pondering is `bonzo.remove()/empty()` -> `bean.remove()` which other (integrated) libraries have the ability to handle nicely. If you remove an element from the DOM it'd be nice if event listeners could be removed from it at the same time.

Of course you don't want to create a dependency between Bonzo and Bean but perhaps a lose messaging/eventing system would solve the problem nicely? Issue #53 ("Before and after package load callbacks") is similar in this regard and could be addressed with the same solution.

I'm thinking perhaps an events module that's provided by Ender to allow notification of internal events. Both Bonzo and Bean could check for the module and if it exists in the environment it, Bonzo could fire a 'removeElement' event with the element as a parameter, Bean could be subscribed to this kind of event and know to look up the element parameter and check if it or any sub-elements have listeners attached and then remove them. And of course it may no even be Bonzo sending or Bean receiving those particular events, there'd develop a de facto standard that other modules could implement if they wanted to participate in the same events, so a Bonzo-replacement could do exactly the same thing to get Bean to clean up.

Ender could also trigger events itself, like 'loadModule' events (as per issue #53) and even 'initComplete' that could be used by modules to do things that can only be done after all modules are loaded (such as checking for the existence of out-of-order optional dependencies).

I imagine you'd argue that this sort of service ought to be provided by an external module but then it'd be difficult to provide internal events about modules being loaded or initialisation being complete. Plus that module would have to be loaded for Bonzo and Bean to talk to each-other so do you make it a hard dependency for them or an optional one where element/event clean up isn't performed if the module isn't included, plus there's the general ordering issue of soft dependencies, what if the eventing module is included after Bean and/or Bonzo and since you have no way to tell when Ender is finished loading, do you have to do a hack like `setTimeout(fn,0)` just to check for the existence of the optional module? (This is where an 'initComplete' would be handy, only at that point do you check if an optional dependency exists in the build).

Thoughts?