Skip to content

Instantly share code, notes, and snippets.

@brian428
Created November 2, 2010 03:12
Show Gist options
  • Save brian428/659216 to your computer and use it in GitHub Desktop.
Save brian428/659216 to your computer and use it in GitHub Desktop.
public function doAsyncChain() : void
{
var eventChain : EventChain = new EventChain( dispatcher );
eventChain.addStep( new EventChainStep( new UserEvent( UserEvent.LOAD_USER ) ) );
eventChain.addStep( new EventChainStep( new UserEvent( UserEvent.USER_PROCESSING_COMPLETE ) ) );
eventChain.start();
}
[EventHandler( event="UserEvent.LOAD_USER" )]
/**
* Since this event handler method returns an AsyncToken, Swiz automatically knows that
* if the method runs as part of an event chain, it should wait until the asyncronous call
* completes before proceeding to the next chain step.
*/
public function loadUser() : AsyncToken
{
return serviceHelper.executeServiceCall( service.loadUser(), handleUserLoadResult );
}
private function handleUserLoadResult( event : ResultEvent ) : void
{
// process the result
}
[EventHandler( event="UserEvent.USER_PROCESSING_COMPLETE " )
public function userProcessingComplete() : void
{
// perform logic or dispatch events when the processing finishes
}
public function doURLRequestChain() : void
{
var eventChain : EventChain = new EventChain( dispatcher );
eventChain.addStep( new EventChainStep( new UserEvent( UserEvent.LOAD_USER_IMAGE ) ) );
eventChain.addStep( new EventChainStep( new UserEvent( UserEvent.USER_PROCESSING_COMPLETE ) ) );
eventChain.start();
}
[EventHandler( event="UserEvent.LOAD_USER_IMAGE" )]
/**
* Load an image. Since this event handler method returns an IAsynchronousOperation,
* Swiz automatically knows that if the method runs as part of an event chain, it should wait
* until the operation completes before proceeding to the next chain step.
*/
public function loadUserImage( event : UserEvent ) : IAsynchronousOperation
{
var request : URLRequest = new URLRequest( targetImagePath );
var loader : URLLoader = urlRequestHelper.executeURLRequest( request, handleUserImageResult );
return new AsynchronousIOOperation( loader );
}
private function handleUserImageResult( event : Event ) : void
{
// process the result
}
[EventHandler( event="UserEvent.USER_PROCESSING_COMPLETE " )
public function userProcessingComplete() : void
{
// perform logic or dispatch events when the processing finishes
}
import org.swizframework.utils.async.AsynchronousEvent;
/**
* By extending AsynchronousEvent (or implementing IAsynchronousEvent), the event can keep track
* of the chain step it is associated with. This allows the event to participate in asynchronous
* event chains.
*/
public class UserEvent extends AsynchronousEvent
{
public static const LOAD_USER : String = "UserEvent.LOAD_USER";
public static const LOAD_USER_IMAGE : String = "UserEvent.LOAD_USER_IMAGE";
public static const USER_PROCESSING_COMPLETE : String = "UserEvent.USER_PROCESSING_COMPLETE";
public function UserEvent( type : String )
{
super( type, true, false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment