Since the existance of Events, EventDispatchers and discussions debating whether weak event listeners are good or bad to use – it’s caused Flash Developers a serious headache. When is it best to use them? When MUST I specify to use weak event listeners?

So just to go ahead an answer some of those…

Well events, as always, you’ll have to use them for User Input controls, such as keyboard, mouse – as well as Streaming data from the microphone, camera, network, etc.  It’s what Flash handles under the hood, so don’t look anywhere else to find a way around it!

As for weak event listeners – boy. That’s still an unfinished debate if you ask me, but I think Robert Penner shed some light with his great Open Source library “Signals”:

http://robertpenner.com/flashblog/2009/09/my-new-as3-event-system-signals.html

Basically, this library makes it easier for C# developers to jump into the ActionScript world.  Each events that you declare basically has an add/remove method, similar to how C# events support the += and -= operator to attach or take off a method to callback when the given event is triggered.

So that’s neat, but like any Open Source project, it’ll grow…  it’ll be jam-pack with goodness oh yes… but probably most of it won’t be of any use for you. You just have to see for yourself!

What are you to do then… if you’re filled with disgust when you think about Event Listeners… when you’re too tired to worry about weak-references, or download a third-party library… hmm…

How about…

“Flash Callbacks!”

Ok so its not the catchiest pick-up line to get a that girl at Starbucks to go head over heals for you, but it will keep you happy like she gave you her number.

In theory, functions can be passed by reference, anywhere, anytime, to just about anything. They can be stored in a single variable, as an element in an array or vector, to a dynamic property of an object, as a key in a dictionary or a value of the key.

So knowing all these possible storage techniques that can be applied to functions, why the hell don’t we create a class to store all the function-references that belong to a particular event and trigger it when appropriate?

Here’s a quick how-to setup:

package  
{
	/**
	 * Flash Callback
	 * @author Pierre Chamberlain
	 */
	public class Callback
	{
		private var _funcs:Array =	null;
		
		public function Callback() 
		{
			_funcs = [];
		}
		
		public function add( pFunc:Function ):void {
			_funcs.push( pFunc );
		}
		
		public function remove( pFunc:Function = null ):void {
			if (pFunc == null) {
				//Clear all:
				_funcs.length =	0;
				return;
			}
			
			var funcID:int =	0;
			while ((funcID = _funcs.indexOf(pFunc)) > -1) {
				_funcs.splice(funcID, 1);
			}
		}
		
		public function run( ... args ):void {
			var func:Function;
			var f:int = 0, fLen:int = _funcs.length;
			
			//If no arguments were supplied...
			if (args == null || args.length==0) {
				for (; f < fLen; f++ ) {
					func =	_funcs[f] as Function;
					//Just call the methods directly:
					func();
				}
			} else {
				//Otherwise, if we have arguments to pass...
				for (; f < fLen; f++ ) {
					func =	_funcs[f] as Function;
					//Apply the function with the supplied arguments:
					func.apply( null, args );
				}
			}
		}
		
		public function get length():int {
			return _funcs.length;
		}
		
	}

}

That short-and-sweet class should be a bit self explanatory. Essentially, with this, you can hook functions from any class to any class. Here’s an example of how to use it:

var burp:Callback = new Callback();
function onBurp( pLoudness:Number ):void {
	if(pLoudness > 0.5) {
		trace("Gross! Please excuse yourself!");
	} else {
		trace("**gives an evil look waiting for an apologetic response**");
	}
}

burp.add( onBurp );

//Somewhere down the road...
burp.run( 2 );
//traces: "Gross! Please excuse yourself!"

//Somewhere else down the road...
burp.run( 0.1 );
//traces: "**gives an evil look waiting for an apologetic response**"

If you have any questions regarding this, just leave a comment and I’ll try to get back to you ASAP :)