Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Conditional AsyncCommands  (Read 4106 times)
psyflex
Jr. Member
**
Posts: 12


View Profile Email
« on: November 04, 2011, 08:21:49 »

Hey everyone !

When I started using AsyncCommands I encountered the situation where if I have a AsyncMacroCommand and one of its commands fails, the Macro keep on running.

This was problematic for me when some of the commands was depended on the failed commands and I wanted to stop the Macro flow when that happen.

So for solving that I wrote an extension to the AsyncCommand utility which I call ConditionalAsyncCommand.

I basically took the AsyncCommand and AsyncMacroCommand and added a boolean to the NextCommand method.

That way when an async command knows about a failure in the Proxy it's waiting for, it can send s onComplete(false) and that way the Macro would stop the loop.

I have 2 questions:

1. If it sounds like a valid solution for that problem? I've tested it and it works for me...

2. If it is, how can I publish it as an open source so it could benefit others (never done that before, so I don't really know about copyright issues and such)


Thanks a lot !
Shai
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: November 05, 2011, 06:03:32 »

Sounds like this might be better made as a modification to the existing AsyncCommand utility. Seems like an option the utility should have. Can you post the modifications? I imagine they would be pretty straightforward.

-=Cliff>
Logged
psyflex
Jr. Member
**
Posts: 12


View Profile Email
« Reply #2 on: November 05, 2011, 11:09:11 »

Absolutely !
Please let me know if I'm missing something.

I'm posting only the modifications:


Modifications to MacroAsyncCommand -

:


                private function nextCommand ( shouldContinue:Boolean = true ) : void
{
if ( shouldContinue && subCommands.length > 0)
{
var commandClassRef:Class = subCommands.shift();
var commandInstance:Object = new commandClassRef();
var isAsync:Boolean = commandInstance is IAsyncCommand;

if (isAsync) IAsyncCommand( commandInstance ).setOnComplete( nextCommand );

ICommand( commandInstance ).initializeNotifier( multitonKey );
ICommand( commandInstance ).execute( note );
if (!isAsync) nextCommand();
}
else
{
if( onComplete != null ){
onComplete( shouldContinue );
}
subCommands = [];
note = null;
onComplete = null;
}
}



Modifications to AsyncCommand -

:

                 /**
* Notify the parent <code>AsyncMacroCommand</code> that this command is complete and it can continue the macro.
*/
protected function commandComplete () : void
{
onComplete( true );
}

/**
* Notify the parent <code>AsyncMacroCommand</code> that this command is complete but it should stop the macro.
*/
protected function stopMacro () : void
{
onComplete( false );
}



Thanks again,
Shai Reznik
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: November 07, 2011, 08:21:35 »

I will check this out this week and see if it poses any backward compatibility issues with the existing AsyncCommand. If not I'll suggest to Duncan that it be integrated into the utility.

Thanks for contributing!

Cheers,
-=Cliff>
Logged
psyflex
Jr. Member
**
Posts: 12


View Profile Email
« Reply #4 on: November 07, 2011, 08:53:14 »

Sure thing!

Thanks for this platform and for taking the time to review my modifications.

Best Regards,
Shai Reznik
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #5 on: November 08, 2011, 08:27:22 »

Hi Shai,

I've spoken with Duncan, the author of the utility and actually he happens to be working on a project where they have made almost the same modifications to AsyncCommand inhouse! Great minds think alike, eh?

Anyway he mentioned they'd run into another rare issue with the AsyncCommand where, due to the fact that nothing has a reference to it, it is possible for it to be garbage collected before it is complete. Of course this wouldn't happen during the synchronous execution of a SimpleCommand, but since an AsyncCommand can be 'hanging' out there waiting for a timer or some other Async response, it is possible for it to look ripe for GC. This was taken care of by having the AsyncCommand add a reference to itself to a static array/map on creation and remove it on complete. Handled by the utility class, subclasses need not even care about this.

This week, we'll get the new code into the repository with your change and this fix, and release a new version.

Thanks for bringing this up.

Cheers,
-=Cliff>
Logged
psyflex
Jr. Member
**
Posts: 12


View Profile Email
« Reply #6 on: November 08, 2011, 09:08:00 »

Great !

Yeah that's actually a good point, I thought that if you pass a reference to an object's function you keep it in the reference count, but as I understand from what you say it isn't the case with server calls.

Looking forward for the new version.
Thanks!
Shai
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #7 on: November 08, 2011, 09:58:58 »

Yeah, something actually has to be hanging on to the reference to the class instance itself as well as the callback method.

This is how the PureMVC Observer class works. I have a new book coming out through O'Reilly in the next month or so, where I show how to handle async operations from SimpleCommands to both the Model and View tier using a subclass of Observer as a formal request object. The Observer keeps the SimpleCommand alive until it gets a response, providing a decoupled way for it to be called back and as a convenient vehicle for request and response data. It doesn't obviate the occasional need for the AsyncCommand if it is part of an AsyncMacroCommand, but it is a different approach.

-=Cliff>
Logged
Pages: [1]
Print