Anonymous Events - Extremely Loose Coupling

In the custom event examples I've seen, there is a subject that is capable of firing an event or many different events. An observer can subscribe to a particular event by registering this interest with the subject. In this type of implementation, the subject must exist first. I came across a situation where it was handy for observers to subscribe to events before a subject even existed capable of firing the particular event. Surely someone has done this before me but I haven't seen a reference to this recipe.

The code for anonymous events is very small. An event manager object can take subscriptions to any string that represents an event. Any object can also tell the event manager to fire the event. When subscribing, the subscriber registers which of it's methods should be called when the event is fired.

eventManager.events = {};

eventManager.subscribe = function(sEvent, oSubscriber, sMethod) {
  if (!eventManager.events[sEvent]) {
    eventManager.events[sEvent] = [];
  }
  eventManager.events[sEvent].push({subscriber: oSubscriber, method: sMethod});
};

eventManager.unsubscribe = function(sEvent, oSubscriber) {
  if (!eventManager.events[sEvent]) {
    return;
  }
  for (var i=eventManager.events[sEvent].length-1; i>=0; i--) {
    if (eventManager.events[sEvent][i].subscriber===oSubscriber) {
      eventManager.events[sEvent].splice(i,1);
    }
  }
};

eventManager.fire = function(sEvent) {
  if (!eventManager.events[sEvent]) {
    return;
  }
  for (var i=0; i<eventManager.events[sEvent].length; i++) {
    subscription = eventManager.events[sEvent][i];
    subscription.subscriber[subscription.method].apply(subscription.subscriber, arguments);
  }
};

The above code is included in the small anonymous events example

The observer pattern is praised for allowing loose coupling between objects. Anonymous events are extremely loose coupling.

Comments

Have something to write? Comment on this article.

Timo Reitz February 22, 2010

The name of the pattern is "Data Bus Pattern" (http://c2.com/cgi-bin/wiki?DataBusPattern).

Krasimir Tsonev February 13, 2011

Hello, I just came to this post. Before a couple of weeks I manage to solve similar problem. Check my EventBus class. I’ll be glad if it helps to someone else :)

http://krasimirtsonev.com/blog/article/javascript-managing-events-dispatch-listen

Karol Kowalski June 6, 2011

I think it's a variation on pub/sub pattern. In my work I ended up with this kind of API:

//jogger
E.subscribe('weather.sunny', jogger.goRunning)
E.subscribe('weather.rainy', jogger.stayAtHome)

//weather
E.publish('weather.rainy')
E.publish('weather.sunny')

In first implementation I also passed object and a method to call on this object, but I now favor making sure the function is going to be called in the right context as a more JavaScript-like approach.

Have something to write? Comment on this article.