[code] GDispatcher: Event swiss army knife.

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)

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

10 Comments

  1. Ahh, the power of G. I’ll play around with it tonight.

  2. Very nice Grant! MMs EventDispatcher was nice but your additions make it REALLY nice. Thanks for sharing this the community!

    I am to pretty deep into an MX2004 project right now and am affraid of rewiring things to use GDispatcher at this point but I will for sure be trying it out on the next project I start!!

    Thanks Grant!

  3. I’d rather use an enumerated type for events, instead of String

  4. I hope I am not stepping on any toes here. I went ahead and developed my own EventDispatcher for AS1 simulating the F04 model. I want to use the same model for F6 components.

    I thought you might like to look at the code; it might offer you some ideas for your gDispatcher. Also, feel welcome to blast my implementation. I Haven’t even downloaded the Demo vers. of F04, so I haven’t seen the actual code for Macromedia’s EventDispatcher. I guess this means my StEventDispatcher will stay in AS1 component form for the time being.

    I have an EventDispatcher demo application that you might find interesting.

    StEventDispatcher code, app and reference info is all online here:

    http://www.shocktime.com/eventdispatcher

  5. String is better how could u use an enumerated type for events ? It’s not clear!!!

  6. EventDispatcher Usage Revisited

    I had a problem in an app I was building at home for fun in Central. As the app got more complex, I started adding more and more events that I needed my AppController to keep track of. AppController being…

  7. You should add another method .addUniqueEventListener( event, scope, method )

    Basically, it’s a helper function that would just check if the listener already exists and adds it if it doesn’t. Would just lend itself to cleaner code.

    Cheers Grant,

    chicken

  8. I’ve uploaded a newer version with a more open license. I also plan to release this code on OSFlash in the very near future. In the meantime, please use the link above to get the latest version.

  9. JUST what I was looking for. I googled “eventlistenerexits AS3” and found these gems.

    Thanks as usual G

    -Danny

  10. Very good site. You are doing a great job. Please keep it up!

Comments are closed.