A few posts back, I mentioned that I was planning to extend the EventDispatcher class to add some features that I wanted. I quickly realized that to satisfy my needs, I would need to do a full re-write, and GDispatcher was born. Being the generous fellow that I am ;), I thought I'd share it with the community - read through this post to download it.

GDispatcher is a new event dispatching class for ActionScript 2.0. It fully implements the EventDispatcher interface, allowing it to be interchanged easily (yay polymorphism!). Beyond the basic functionality found in EventDispatcher (described here), it also supports these new features:
  • Listeners can subscribe to all events from a dispatcher
  • Listeners can specify a function to handle specific events, or all events (within the scope of the listener)
  • Can check if a listener is already subscribed to an event
  • Can remove all listeners for a specific event or for all events

Following, is some limited documentation on using the new features. When I get some time, I will write up a post on actually implementing it in a project, though you can get a pretty good idea by reading my last couple of posts on the subject (Using EventDispatcher, EventDispatcher: the next morning).

Subscribing to all events:
This can be done just as though you were subscribing to any other event, but you pass "ALL" instead of an event name:

someObject.addEventListener("ALL",this);

You can also remove a listener for all events as with any other event:

someObject.removeEventListener("ALL",this);

Note that you are really subscribing to an ALL event, that is triggered by any event call from the dispatching object (someObject). If you subscribe to other specific events from the same object, you will receive duplicate calls. Similarly, you cannot subscribe only to the ALL event, and then remove specific events.

Specifying a handler function:
By passing an optional third parameter in an addEventListener call, you can specify the name of a function to call. This function will be called within the scope of the object specified in the second parameter:

someObject.addEventListener("click",this,"onClicked");

This can of course be combined with other new features, like listening to "ALL" events:

someObject.addEventListener("ALL",this,"onSomeEvent");

To remove the listener, all parameters must be the same:

// will not remove the listener:
someObject.removeEventListener("ALL",this);
// will remove it:
someObject.removeEventListener("ALL",this,"onSomeEvent");

Checking for a listener:
GDispatcher adds the "eventListenerExists" method to check if a listener is already subscribed to a certain event:

if (someObject.eventListenerExists("click",this)) {
   trace("Yes, it's subscribed");
} else {
   trace("No, it isn't subscribed");
}

Again, the listener details must match exactly to the details used to subscribe it.

Removing all listeners to an event
The added "removeAllEventListeners" removes all event listeners from a dispatcher for a specific event, or for all events.

For example, to remove all listeners to the click event for someObject:

someObject.removeAllListeners("click");

To remove all listeners that subscribe to the "ALL" event for someObject:

someObject.removeAllListeners("ALL");

To remove every listener for every event for someObject, simply do not specify an event name:

someObject.removeAllListeners();



Downloading GDispatcher
GDispatcher is in beta, but I would like as many people to try it out as possible. I can't offer support for it, and will warn you in advance that you will need a basic understanding of how EventDispatcher works to use GDispatcher successfully, at least until I get the time to post more in-depth docs.

I would really appreciate it if coders could report bugs, or request features for gDispatcher in the comments, or by emailing them to me (there's an address in the comments at the top of the AS file you can use).

Click here to download GDispatcher. (.zip containing a single .as file, 1.5kb)