SignalResponder

SignalResponder

What?

I wanted to create an easy-to-use helper class that can deal with async process state changes in AS3.
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.

Basic Usage

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 );
}

Advanced usage

Custom signals for specific state changes

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 );
}

Using a custom signal for all state changes

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

Notes

  • 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

Dependencies

Signals by Robert Penner

https://github.com/robertpenner/as3-signals

Advertisements
    • Pavel fljot
    • September 15th, 2011
  1. I’m really not a fan of method(responder):void

    Your example implementation (loadTwitterStream) can only handle one request at a time as it has to store the responder in an instance variable (which gets overwritten on subsequent requests).

    I way prefer method(…params):SignalResponder.

  2. @shaun I knew you’d be saying that 🙂

    As a matter of fact it’s because of a restriction of Signals I didn’t take your preferred usage as an example, since adding a listener to a signal after it has dispatched doesn’t notify the listener that the signal already has dispatched.
    But I created a RelaxedSignal and issued a pull request to Robert Penner.

    You can already use it the way you prefer, but then you need to make sure that it’s an async process. In a synced one, the success or failure signals will dispatch before the listener can be added.

    @Pavel It’s not that I dislike Promises, this is just an alternative, that can be used in different ways than Promises. For one, it doesn’t use Events. Secondly IMO it’s more versatile in potential usages and coding styles.

    One advantage it has over Promises is that since the signals can be injected, it can use system-wide signals w/o the need to relay a SignalResponder specific message to the system.

  3. Indeedy, RelaxedSignal sounds great and would make SignalResponder much better in my opinion.

    SignalResponder looks good, it’s the method(responder):void signature that I despise 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: