
Signal-slot library for AS3.
This tiny library is a result of many optimizations I've attempted with Flash built-in events system. Finally, I decided to abandon all attempts of hacking the built-ins and designed something very simple to replace it.
Here are the main differences:
- When you invoke a handler (i.e. slot) no additional objects are created
- All event types are known beforehand, objects cannot dispatch unforeseen events.
- You can specify the types and number of the arguments the handler (slot) should take.
- target, currentTarget, stopPropagation, handled are not implemented, it is for the programmer, to extend and add that functionality.
- There is nothing like useCapture or bubbling in there just because it doesn't deal with display list.
If you need any suggestions as to where this might be useful, and where this won't be useful for certain:
It would be unwise to try and replace the native events, but if you're adding many custom events, then it might be worth looking in here.
Small projects, where event generation ind dispatching will not likely affect the productivity wouldn't be probably the best target either. The main goal of this library is to save on "magic strings" which are event's metadata and event's constants usually used with built-in events, however, if there aren't to many of those, then it is unlikely that you will see any benefits of using signals.
Here's the usage example:
Code:
package signals
{
//{ imports
import flash.display.Sprite;
import org.wvxvws.signals.ISemaphore;
import org.wvxvws.signals.SignalError;
import org.wvxvws.signals.Signals;
//}
/**
* TestAsteriscNamespace class.
* @author wvxvw
* @langVersion 3.0
* @playerVersion 10.0.32
*/
public class SignalsExample extends Sprite implements ISemaphore
{
//--------------------------------------------------------------------------
//
// Public properties
//
//--------------------------------------------------------------------------
public static const FOO:Vector.<Class> = new <Class>[int];
public static const BAR:Vector.<Class> = new <Class>[String, int];
/* INTERFACE org.wvxvws.signals.ISemaphore */
public function get signals():Signals { return this._signals; }
//--------------------------------------------------------------------------
//
// Protected properties
//
//--------------------------------------------------------------------------
protected var _signals:Signals;
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public function SignalsExample()
{
super();
this._signals = new Signals(this);
this._signals.add(FOO, this.slotTest3);
this._signals.add(BAR, this.slotTest);
this._signals.add(BAR, this.slotTest2);
this._signals.call(BAR, "Foo", 100);
this._signals.call(FOO, 200);
try
{
this._signals.call(FOO, "Foo", 100);
}
catch (error:SignalError)
{
// Attempting to call slot with wrong signature.
trace(error.message);
}
}
//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------
/* INTERFACE org.wvxvws.signals.ISemaphore */
public function signalTypes():Vector.<Vector.<Class>>
{
return new <Vector.<Class>>[FOO, BAR];
}
//--------------------------------------------------------------------------
//
// Private methods
//
//--------------------------------------------------------------------------
private function slotTest(par0:String, par1:int):void
{
trace("slotTest called", par0, par1);
}
private function slotTest2(par0:String, par1:int):void
{
trace("slotTest2 called", par0, par1);
}
private function slotTest3(par1:int):void
{
trace("slotTest3 called", par1);
}
}
}
The library is here:
http://code.google.com/p/e4xu/source/br ... ws/signalsIt isn't yet available as SWC, but I'll make one once I'm 100% sure on the interfaces (so, there are potential changes on the way).
Looking forward to hear your feedback! Enjoy
EDIT: Slot signature is verified when called.EDIT: Removed SignalType class, made it a little bit smaller.EDIT: Some performance optimization added (but can optimize more!).