Decoupled Components – Communication Using Events

designdesign-patternsjavascriptobject-orientedobject-oriented-design

We've got a Web App where we have a lot (>50) of little WebComponents that interact with each other.

To keep everything decoupled, we have as a rule that no component can directly reference another. Instead, components fire events which are then (in the "main" app) wired to call another component's methods.

As time went by more and more components where added and the "main" app file became littered with code chunks looking like this:

buttonsToolbar.addEventListener('request-toggle-contact-form-modal', () => {
  contactForm.toggle()
})

buttonsToolbar.addEventListener('request-toggle-bug-reporter-modal', () => {
  bugReporter.toggle()
})

// ... etc

To ameliorate this we grouped similar functionality together, in a Class, name it something relevant, pass the participating elements when instantiating and handle the "wiring" within the Class, like so:

class Contact {
  constructor(contactForm, bugReporter, buttonsToolbar) {
    this.contactForm = contactForm
    this.bugReporterForm = bugReporterForm
    this.buttonsToolbar = buttonsToolbar

    this.buttonsToolbar
      .addEventListener('request-toggle-contact-form-modal', () => {
        this.toggleContactForm()
      })

    this.buttonsToolbar
      .addEventListener('request-toggle-bug-reporter-modal', () => {
        this.toggleBugReporterForm()
      })
  }

  toggleContactForm() {
    this.contactForm.toggle()
  }

  toggleBugReporterForm() {
    this.bugReporterForm.toggle()
  }
}

and we instantiate like so:

<html>
  <contact-form></contact-form>
  <bug-reporter></bug-reporter>

  <script>
    const contact = new Contact(
      document.querySelector('contact-form'),
      document.querySelector('bug-form')
    )
  </script>
</html>

I'm really weary of introducing patterns of my own, especially ones that aren't really OOP-y since I'm using Classes as mere initialisation containers, for lack of a better word.

Is there a better/more well known defined pattern for handling this type of tasks that I'm missing?

Best Answer

The code you have is pretty good. The thing that seems a bit off-putting is the initialization code is not part of the object itself. That is, you can instantiate an object, but if you forget to call its wiring class, it's useless.

Consider a Notification Center (aka Event Bus) defined something like this:

class NotificationCenter(){
    constructor(){
        this.dictionary = {}
    }
    register(message, callback){
        if not this.dictionary.contains(message){
            this.dictionary[message] = []
        }
        this.dictionary[message].append(callback)
    }
    notify(message, payload){
        if this.dictionary.contains(message){
            for each callback in this.dictionary[message]{
                callback(payload)
            }
        }
    }
}

This is a DIY multi-dispatch event handler. You would then be able to do your own wiring by simply requiring a NotificationCenter as a constructor argument. Sending messages into it and waiting for it to pass you payloads is the only contact you have with the system, so it's very SOLID.

class Toolbar{
    constructor(notificationCenter){
        this.NC = notificationCenter
        this.NC.register('request-toggle-contact-form-modal', (payload) => {
            this.toggleContactForm(payload)
          }
    }
    toolbarButtonClicked(e){
        this.NC.notify('toolbar-button-click-event', e)
    }
}

Note: I used in-place string literals for keys to be consistent with the style used in the question and for simplicity. This is not advisable due to risk of typos. Instead, consider using an enumeration or string constants.

In the above code, the Toolbar is responsible for letting the NotificationCenter know what type of events it's interested in, and publishing all of its external interactions via the notify method. Any other class interested in the toolbar-button-click-event would simply register for it in its constructor.

Interesting variations on this pattern include:

  • Using multiple NCs to handle different parts of the system
  • Having the Notify method spawn off a thread for each notification, rather than blocking serially
  • Using a priority list rather than a regular list inside the NC to guarantee a partial ordering on which components get notified first
  • Register returning an ID which can be used to Unregister later
  • Skip the message argument and just dispatch based on the message's class/type

Interesting features include:

  • Instrumenting the NC is as easy as registering loggers to print payloads
  • Testing one or more components interacting is simply a matter of instantiating them, adding listeners for the expected results, and sending in messages
  • Adding new components listening for old messages is trivial
  • Adding new components sending messages to old ones is trivial

Interesting gotchas and possible remedies include:

  • Events triggering other events can get confusing.
    • Include a sender ID in the event to pinpoint the source of an unexpected event.
  • Each component has no idea whether any given part of the system is up and running before it receives an event, so early messages may be dropped.
    • This may be handled by the code creating the components sending a 'system ready' message , which of course interested components would need to register for.
  • The event bus creates an implied interface between components, meaning there is no way for the compiler to be sure you've implemented everything you should.
    • The standard arguments between static and dynamic apply here.
  • This approach groups together components, not necessarily behavior. Tracing events through the system may require more work here than the OP's approach. For example, OP could have all of the saving-related listeners set up together and the deleting-related listeners set up together elsewhere.
    • This can be mitigated with good event naming and documentation such as a flow chart. (Yes, the documentation is famously out of step with the code). You could also add pre- and post- catchall handler lists that get all messages and print out who sent what in which order.
Related Topic