You can contact me on Twitter with questions/remarks : @camillereynders.
Signals by Robert Penner
I wanted to create an easy-to-use helper class that can deal with async process state changes.
A SignalResponder allows you to easily respond to start, progress, success and failure signals.
It’s meant to be a versatile class, that can be used in many different ways, allowing to use them
in any way you see fit.
Consumption of a process result through a SignalResponder
_responder = new SignalResponder(); _responder.successSignal.addOnce( _handleSuccessSignal ); _service = new TwitterService(); _service.loadTwitterStream( _responder ); private function _handleSuccessSignal( data : String ):void { trace( data ); }
Usage of a SignalResponder in the process
public function loadTwitterStream( responder : SignalResponder ) : void{ var request : URLRequest = new URLRequest( TWITTER_FEED_URL ); _loader = new URLLoader(); _loader.addEventListener( Event.COMPLETE, _handleCompleteEvent ); _loader.load( request ); _responder = responder; } private function _handleCompleteEvent( e : Event ) : void{ _responder.successSignal.dispatch( _loader.data ); }
A SignalResponder can use custom signals for specific state changes
_responder = new SignalResponder(); _responder.successSignal = new TwitterStreamLoadedSignal(); _responder.successSignal.addOnce( _handleSuccessSignal ); _service = new TwitterService(); _service.loadTwitterStream( _responder ); private function _handleSuccessSignal( data : String ):void { trace( data ); }
SignalResponder uses a ISignalFactory to create the signals for the various state changes.
By default the factory creates signals of type Signal, but this can easily be adjusted.
var factory : ISignalFactory = new SignalFactory( TwitterServiceStateChangeSignal ); var responder : SignalResponder = new SignalResponder( factory ); //now all 4 signals (startSignal, progressSignal, successSignal and failureSignal ) will be of //type TwitterServiceStateChangeSignal
- The signals are instantiated lazily, if a process doesn’t dispatch a certain signal and there
were no listeners specified for that particular signal, then the signal will not be instantiated.
This means that in cases as above in the example of Basic Usage only one signal is instantiated. - A SignalResponder has a `result` property which contains the result of the process after
completion and is bindable. - The `status` property contains the current process state expressed as a constant from
SignalResponderStatus