Hotwire: Quick and dirty event wireups in AS3
Posted on 23. Feb, 2010 by lukesh in Flash Platform, Technology and Science
Recently, I was tasked with building a quick AS3 project outside of our normal Flex framework projects. It was refreshing to just write a plain and simple AS3 project outside of the team environment where I could explore a few things without impacting the team by breaking our current project standards. One thing I’ve been wanting to explore is Penner’s Signals project, but after looking at it, I decided that: 1. it was still too heavy for my project, 2. I didn’t see a way to listen to events that Flash dispatched–mouse events, stage events, etc. I’ve been using a derivative of EventManager for a while, and it’s excellent, but I wanted to explore a different strategy for managing events, so I ended up writing Hotwire.
Some goals I wanted to achieve were:
- As little code as possible to wire up and unwire events.
- Lean more toward convention over configuration.
- Easy to see which events were being listened for (remotely like the delegate concept in Objective-C)
- It’s not a goal, but I didn’t care about proper use of accessors (one drawback of Hotwire)
The source for Hotwire is 62 lines long, so you could probably clone it and analyze it quicker than I can describe it, but I’ll outline a few use cases anyway.
Usage 1: As Simple As Possible
This is as basic as it gets. You store a reference to an instance of Hotwire, which sets up at least a Event.REMOVE listener, then you call unwire(), which removes all the event listeners setup by the wire() call.
By default, Hotwire attempts to register the event with a listener with the prefix “handle” then a properly-cased version of the event name. Therefore, since Event.ENTER_FRAME is the string “enterFrame”, the listener that Hotwire will wire up is “handleEnterFrame”.
Usage 2: Listening to Other Dispatchers
In the Hotwire constructor, the first argument is the dispatcher, and is the only required argument. The second argument is the handler, and if it is not specified or null, Hotwire will consider the handler and the dispatcher the same object. In my projects, I used custom bubbled events a-plenty for child display objects, and never specified a different handler from my dispatcher until I needed to handle Stage events, which don’t bubble. (I haven’t run across another instance where I couldn’t simply use bubbled events the way I wanted to, but I’m sure I will.)
Usage 3: If you don’t like “handle”
Personally, I like the word “handle” as my handler prefix. I can type handle+Ctrl+Space and get a list of all my handlers, and it’s descriptive. I know that many people like to use “on” however. If you want to change your prefix for one instance, it’s the third argument in the constructor, as you may have noticed in the example above. If you hate “handle” just change the source code. It’s 62 lines. Don’t hate.
Finally
So that’s it. I think Hotwire might be useful if you wanted to wire / unwire groups of events easily also, just by creating separate instances of Hotwire. (Although, I might argue your class is getting too complicated?)
There are some cons to Hotwire that may make your architecture blood boil (for some reason I’ve been much less OCD about all this, so I’m just letting it roll and enjoying the short time of non-obsession):
- Public handlers. Yeah, if you make private or protected handlers, the instance of Hotwire can’t wire them up. Well, hey. It’s a mixin-y class. Put some Baileys in your latté and discuss architecture with me when I come back to my senses.
- Handler names derived from constants. This could be pretty bad if you are wiring up to Event.ENTER_FRAME which is currently “enterFrame” but Adobe decides to change to “zomgAppleSucks”. Yes, this might be a problem. In the remote chance this happens, :%s/handleEnterFrame/handleZomgAppleSucks/g and go back to your latté.
If you find it useful, let me know. If you have improvements, fork it. If you think it’s horrible and want to throw your latté at me, please do, I love constructive criticism and alternative perspectives. I’ll just passively-aggressively unfollow you on Twitter.
If you still aren’t clear on how to get Hotwire, even though I linked to it like a million 3 times, it’s hosted on GitHub, and you can get it here.



