Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
December 15, 2017, 06:26:43 PM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: 1 2 3 [4]
Print
Author Topic: AsyncCommand - A PureMVC / AS3 Utility  (Read 48247 times)
Neil Manuell
Courseware Beta
Sr. Member
***
Posts: 109


View Profile Email
« Reply #45 on: May 19, 2009, 03:55:58 AM »

true Smiley
Logged
skasssam
Jr. Member
**
Posts: 18


View Profile
« Reply #46 on: October 26, 2012, 06:25:58 AM »

I hope I'm not beating a dead horse but I am really looking for some guidance here.

I have certain situations where I need to call a certain sequence of Proxy calls (same proxy different methods) and they must be completed in a certain order.

I have it currently implemented like:
Mediator A -> Proxy A.1 -> Mediator A-> Proxy A.2 -> Mediator B -> Proxy A.3

I thought there was a better way to do this since following a trail of Notifications doesn't seem right.

Initially it looked like AsyncCommand could help me with something like this but after reading the thread I'm not sure.

I was hoping that the AsyncCommand could help me do something like this:
Mediator A -> AsyncCommand

My AsyncCommand would have AsyncSubCommands that would have calls to the ProxyA.1, A.2....

Is this a good use case for the AsyncCommand to help me get rid of the trail (and back and forth between mediator and proxy) of notifications?

TIA
Shinan
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #47 on: October 26, 2012, 08:32:08 AM »

Yes, it is a good approach. However if you need to do something with the result of the first call before making the second call, (such as extracting the args for the next call), then you might run into trouble since the command won't hear any notifications coming back from the Proxy, and should not place listeners on the Proxy's service.

Another good approach that takes care of this (and doesn't require AsyncCommand) is the ServiceRequest pattern, which I describe in the O'Reilly book [1].

Essentially, a normal Command can send a formal request (based on Observer, and containing a callback to the command) to a Proxy method. The Proxy hangs onto the request (keeping the Command in memory since it has a reference). When the service returns, the Proxy sends the data back to the caller (the Command), which can then make the next request. You could make all your calls from the same SimpleCommand OR you could have a MacroCommand with three SimpleCommands that all do this. The first subcommand could set the results in the body of the note for the second subcommand to use and so on.

Although I think you'd find the explanation in the book more complete, here are some links to the relevant code:

The ServiceRequest class:
http://examples.oreilly.com/0636920022459/StoryArchitect/src/com/futurescale/sa/controller/command/story/SearchStoryCommand.as

A Command that creates the request:
http://examples.oreilly.com/0636920022459/StoryArchitect/src/com/futurescale/sa/controller/command/story/SearchStoryCommand.as

A Proxy that handles the request:
http://examples.oreilly.com/0636920022459/StoryArchitectModel/src/com/futurescale/sa/model/proxy/StoryReadingProxy.as

Also, this formal request pattern works for any async operation, and the application in the book also makes use of it to allow commands to request a popup and get called back with whatever the user's choices/input were. This makes it a really useful pattern.

Just for fun, here's the popup subsystem. Notice how the AbstractPopupMediator handles the request and the subclasses just have to basically return a different popup component and be interested in a particular notification:
http://examples.oreilly.com/0636920022459/StoryArchitect/src/com/futurescale/sa/view/popup/

Cheers,
-=Cliff>
[1] ActionScript Developer's Guide to PureMVC http://oreil.ly/puremvc
Logged
skasssam
Jr. Member
**
Posts: 18


View Profile
« Reply #48 on: October 26, 2012, 08:49:19 AM »

Thanks Cliff,

I have additional questions about the the implementation in the demo.

This makes sense:
Code:
public class ExampleAsyncMacroCommand extends AsyncMacroCommand
{
/**
* Execute the SubCommands.
*/
override protected function initializeAsyncMacroCommand () : void
{
addSubCommand( FirstAsyncCommand );
addSubCommand( SecondAsyncCommand );
addSubCommand( ExampleSimpleCommand );
addSubCommand( ThirdAsyncCommand );
addSubCommand( ExampleSimpleCommand );
}

}

If FirstAsyncCommand is going to call ProxyA.1. How would I know when to call commandComplete in FirstAsyncCommand if ProxyA.1 is an async remote object call? I think I need to call commandComplete() before the next SubCommand will execute. (is this right?)

This is taken from the demo just as a reference:
Code:
public class FirstAsyncCommand extends AsyncCommand
{
/**
* Execute the business logic.
* <P>
* Starts a timer which calls <code>onTimer</code> when complete.
*/
override public function execute ( note:INotification ) : void
{
var timer:Timer = new Timer( 5000, 1 );
timer.addEventListener( TimerEvent.TIMER, onTimer );

var logMessage:String = "STARTING FIRST ASYNC COMMAND...";
sendNotification( ApplicationFacade.LOG_OUTPUT, logMessage );

timer.start();
}

/**
* Handle Timer event.
* <P>
* Resets timer and removes listener, sends a notification
* with a log message and finally, notifies any
* AsyncMacroCommand that may have invoked it that this
* AsyncCommand is complete.
*/
private function onTimer( event:TimerEvent ) : void
{
var t:Timer = event.currentTarget as Timer;
t.reset();
t.removeEventListener( TimerEvent.TIMER, onTimer );
t = null;

var logMessage:String = "FIRST ASYNC COMMAND COMPLETE";
sendNotification( ApplicationFacade.LOG_OUTPUT, logMessage );

commandComplete(); 
}
}

Thanks for your help and I am reading through the other method you suggested.
Shinan
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #49 on: October 27, 2012, 02:16:00 PM »

That's the issue, is getting called back by the Proxy.

The AsyncCommand needs to be set up, for example, as an IResponder and placed in an AsyncToken.responders on the service call or in some other way arrange to be called back when the service is complete.

I don't like Commands getting that closely involved in the service implementation. IResponder and AsyncToken are Flex service classes - what if you wanted to do this with pure AS3? Changing the service implementation shouldn't affect the application's business logic.

My example above, using a ServiceRequest class that extends Observer, keeps it all in-framework and provides the best way I've found to have a Command make an async request and be called back.
Logged
skasssam
Jr. Member
**
Posts: 18


View Profile
« Reply #50 on: October 29, 2012, 05:24:22 AM »

Thanks Cliff,
I saw what you did in the example and it makes sense. Thanks! That is the methodology I am going to use.
Logged
Pages: 1 2 3 [4]
Print
Jump to: