- Objects MUST extend EventDispatcher in order to be able to dispatch events. Implementing IEventDispatcher alone is impossible because the event target is read-only and you cannot change it from outside of EventDispatcher.
- It’s difficult to manage events. There’s no way you can remove all event handlers attached to an event dispatcher at once. So in some cases, you don’t remove them properly and they start causing bugs. (BTW, using weak references is not reliable and not recommended.)
- You cannot pass extra data to you listener unless you extends a new Event object
- You cannot set up your interface in a way that it can enforce implementing classes to dispatch the required events.
- On performance perspective, creating objects is one of the most evil things. That’s why any advanced programmers must know and practically use objects pool. However, we are still wasting a lot of resources creating event objects, especially repetitive events like mouse move or enter frame.
- Read here for more critiques on AS3 events
Here’s the briefing from as3signals wiki page:
Signals: Think Outside the Event.Let’s see how as3signals gains advantages over AS3 events:
Signals are light-weight, strongly-typed AS3 messaging tools.
Wire your application with better APIs and less boilerplate than AS3 Events.
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.
- Signals are inspired by C# events and signals/slots in Qt.
1. Any object can dispatch events
Here’s how you create an event dispatching object with Signals:- import org.osflash.signals.Signal;
- public class Radio {
- // This is the public API to which listeners subscribe.
- public var messageChanged:Signal;
-
- public function Radio() {
- // Create a Signal that dispatches a String.
- }
-
-
- // The setter uses the Signal to dispatch the changed value.
- _message = value;
- messageChanged.dispatch(_message);
- }
- }
- var radio: Radio = new Radio();
- // Hook the listener up to the Radio's Signal.
- radio.messageChanged.add(messageChangedHandler);
- trace("heard message: " + message);
- }
2. With signals, managing events is much less hassle
You can remove all event handlers with one convenient method:signal.removeAll();
You can retrieve number of listeners: signal.numListeners;
You can add a listener that will be removed automatically after just one event: signal.addOnce(theListener); // result: signal has one listener
signal.dispatch(theEvent); // result: theListener is called, signal now has no listeners
signal.dispatch(theEvent); // result: theListener is called, signal now has no listeners
3. You can dispatch with any number of arguments to listener
You can define types for your argumentsLater on, when you dispatch events, as3signals can detect and notify argument error
//later:
progress.dispatch(); // will throw ArgumentError
progress.dispatch('The Answer'); // will throw ArgumentError
progress.dispatch('The Answer', 42.5); // will throw ArgumentError
progress.dispatch('The Answer', 42); // will succeed
When adding listeners, they are all so checked for argument types. progress.dispatch(); // will throw ArgumentError
progress.dispatch('The Answer'); // will throw ArgumentError
progress.dispatch('The Answer', 42.5); // will throw ArgumentError
progress.dispatch('The Answer', 42); // will succeed
4. It’s possible that you enforce events in interface
To see an example, please follow this link http://snipt.org/wSm5. It’s performance gain because no event object is created in Signals
But that’s not everything yet. The ultimate deal is to replace ENTER_FRAME event.As we know that ENTER_FRAME event is the key to time-based animations and are widely used. Normally, ENTER_FRAME event can create 25 – 30 objects per second, that’s 1500-1800 objects per minute. But we are aware that that creating objects is evil. So some guys have tried to replace the ENTER_FRAME event with Signals. Read this blog for more info. (The site is currently down, so please use this cache from Google)
6. Signals only projects
Signals can wrap the native events of AS3 so that the whole project will use a consistent messaging system for communication between components. For example:The author also claimed that he’s experimenting the bubble feature (the ability to forward events among objects in display list).
Conclusion
It is still under development and improvement but as3signals’ current feature set can satisfy most common project’s needs. Some guys even love it so much that they branch some frameworks to work purely with Signals, for example:- PureMVC AS3 Multicore – as3signal branch
- RobotSignals – RobotLegs & as3signals combination
- SWFAddress as3signals version
- See more examples here
Anyway, it is worth trying as3signals in my upcoming projects to see if it really ‘shines’ in real applications.
(Credit: some of the sample code are revised from this Hello World example)
[Vietnamese tags: as3signals: cách tiếp cận mới cho sự kiện (event) trong AS3]
AS3-Signals is great.
ReplyDeleteHere is a slideshow, GIT Code Sample, and screencast video about the fantastic AS3-Signals.
http://www.rivellomultimediaconsulting.com/as3-signals-introduction/