-
Notifications
You must be signed in to change notification settings - Fork 203
Home
Signals is a new approach for AS3 events, inspired by [http://en.wikipedia.org/wiki/C_Sharp_syntax#Events C# events] and [http://en.wikipedia.org/wiki/Signals_and_slots signals/slots] in Qt.
It’s not finished and I am looking for your impressions, critiques and suggestions.
[http://robertpenner.com/flashblog/2009/09/my-new-as3-event-system-signals.html Please post feedback on my blog].
- [http://code.google.com/p/as3-signals/source/browse/#svn/trunk/src/com/robertpenner/signals Browse source code]
- [http://code.google.com/p/as3-signals/source/browse/#svn/trunk/tests/com/robertpenner/signals Browse unit tests]
- [http://code.google.com/p/as3-signals/source/checkout Checkout from SVN]
== Concept ==
- A Signal is essentially a mini-dispatcher specific to one event, with its own array of listeners.
- A Signal gives an event a concrete membership in a class.
- Listeners subscribe to real objects, not to string-based channels.
- Event string constants are no longer needed.
== Syntax ==
- comparison:
{{{
// with EventDispatcher
button.addEventListener(MouseEvent.CLICK, onClick);
// Signal equivalent
button.click.add(onClick);
}}}
- Create a Signal for a class:
{{{
////// in Item class:
// Recommended: make the signal read-only.
protected var _ready:Signal;
public function get ready():Signal { return _ready; }
public function Item()
{
_ready = new Signal(this, ReadyEvent);
}
protected function sendReady():void
{
_ready.dispatch(new ReadyEvent());
}
////// in another class:
var item:Item = new Item();
protected function onItemReady(e:ReadyEvent):void { … }
item.ready.add(onItemReady);
}}}
== Features ==
- Remove all event listeners:
{{{
signal.removeAll();
}}}
- Retrieve the number of listeners:
{{{
var numListeners:uint = signal.length;
}}}
- Listeners can be added for a one-time call and removed automatically on dispatch:
{{{
signal.addOnce(theListener); // result: signal has one listener
signal.dispatch(theEvent); // result: theListener is called, signal now has no listeners
}}}
- Any object type can be dispatched to listeners. But using IEvent will enable full functionality.
- A Signal can be initialized with an event class that will validate event objects on dispatch (optional):
{{{
progress = new Signal(this, ProgressEvent);
//later:
progress.dispatch(new NotProgressEvent()); // will throw ArgumentError
}}}
- If the Signal’s event class is specified, each listener is checked on `add()` to ensure it declares at least one argument.
- Signals can be placed in interfaces to indicate the events dispatched by a class.
- Events can bubble recursively through `.parent` independent of the display list (experimental).
- Code was developed test-first.
== Background on AS3 Events ==
- [http://robertpenner.com/flashblog/2009/08/my-critique-of-as3-events-part-1.html My Critique of AS3 Events – Part 1]
- [http://robertpenner.com/flashblog/2009/09/as3-events-7-things-ive-learned-from.html AS3 Events – 7 things I’ve learned from community]
- [http://robertpenner.com/flashblog/2009/09/my-critique-of-as3-events-part-2.html My Critique of AS3 Events – Part 2]