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: Handling Async Startup Data Dependencies  (Read 12308 times)
JJfutbol
Sr. Member
****
Posts: 53


View Profile WWW Email
« on: April 06, 2009, 06:22:30 »

I'm curious if anyone here has any utilities or approaches they use to handle loading data dependencies at startup. By that I mean making any service calls and waiting for them to complete before any interactions with the app can take place. I use the AsyncCommand utility in my app but its not what I'm looking for in this case since I'm loading dependencies in parallel (not in sequence, each call isn't dependent on one another).

Currently, what I do is have a command called CheckDataDependenciesCommand that fires any time one of those service calls completes. This is nice and simple but I need to handle failures and would like to enable retrying a service call a number of times if it has failed. I'm thinking my simple approach won't really do the trick and maybe a utility is better suited.

Thoughts or suggestions?
Logged
philipSe
Sr. Member
****
Posts: 139


View Profile Email
« Reply #1 on: April 07, 2009, 02:50:27 »

Maybe the Startup Manager is worth a look, if you haven't already...
http://trac.puremvc.org/Utility_AS3_StartupManager
http://forums.puremvc.org/index.php?topic=259.msg985#msg985
----Philip
Logged
Justin
Full Member
***
Posts: 24


View Profile Email
« Reply #2 on: April 08, 2009, 03:04:15 »

If you are using the Flex framework for your application, there is a nice hack you can use to "stall" the application initialization until your dependencies are locked and loaded. 

In your shell, you can override the public setter 'initialized' and insert a conditional that allows the app to initialize only when your dependencies are finished loading.  This is a technique that I use for the very reason you indicated.  The code might look like this:
:
/**
* Flag that is set to true when the final dependency is loaded
*/
protected var _dependenciesLoaded : Boolean;
/**
* Flag that indicates whether or not we are 'waiting' for our dependencies to load
*/
protected var _dependenciesPending : Boolean;
/**
* Override initialized and stall the application until
* all dependencies are loaded
*/
override public function set initialized(value : Boolean) : void
{
    handleDependencies();
}

/**
* Handles dependencies before starting up the application
*/
protected function handleDependencies()
{
    if (!_dependenciesPending)
    {
        facade.startup(this); // Allow PureMVC to start so you gain access to your proxies
        // Code to load all dependencies goes here
        _dependenciesPending = true;
    }
    if (_dependenciesLoaded)
        super.initialized = value; // We have loaded all dependencies, allow the app to start
    else
        callLater(handleDependencies); // Check again on the next frame
}

This is the setter in all UIComponents that dispatches the CREATION_COMPLETE event when set to true by the LayoutManager after all phases of instantiation have completed.  Unless this is set to true, the default Flex preloader for your application will remain on screen in the "Initializing" state preventing all user interaction.

I use this as an alternative to the CREATION_COMPLETE event when starting up Pure MVC in all cases since I want to guarantee Pure MVC is started BEFORE the CREATION_COMPLETE event is dispatched.  I found this to be a bit more reliable since I cannot always have direct control over the order of execution of handlers listening for this event especially in a multi-dev environment.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: April 08, 2009, 05:23:52 »

I'm building an app that will end up as a demo here on the site after FITC. For now it's the basis of my presentation there.

Basically I've been able to totally cope with this using the StateMachine. No need for weird, out-of-band solutions or special magic, this will work on any PureMVC port.

The problem with the current statemachine demo is I haven't had time to update it since the 1.1 release. The addition of the state-specific 'changed' note was all it took.

I have a product that will be out in a few months that is using it and its rock-solid. That's why I'm so confident about this approach.

It goes through multiple async phases at startup, (with potential data gathering/saving loops to fulfill requirements) before getting to the 'lets do stuff' interface.

Being an AIR app, it goes first through a 'checking internet' state, and from there possibly to an 'offline' holding state until the internet comes online (plug the cable back in).

Then on to a 'validating license' state to make sure you have a valid license to operate the program, and possibly to an 'entering license' state followed by a 'saving license' state', and back to 'validating license'. Validation is async, as it checks a publicly available amazon s3 bucket owned by me.

Then on to a 'validating s3 credentials' state (the app requires the user to have their own s3 account to store data), and possibly off to an 'entering s3 creds' followed by a 'saving s3 creds' and back to 'validating s3 creds' state. Of course this is async as well.

If the internet is there and your license is valid and your s3 creds are valid, bang, bang, bang you're into the functionality with no human interaction. But the first time you run it or if your license has expired or been revoked or your internet is offline, or your s3 account has been closed, you'll loop through those states as need be till everything is satisfied.

The magic? There is none. Don't think of startup as this huge chain of commands you need to run and if things go foul, it's going to be crazy hard to handle and assume the happy path. No, this type of setup is PEANUTS (and not the kind sold by Peanut Corp of America) to the StateMachine.

The key is in defining your states as I've described above. And triggering your async processes via the 'changed' attribute of the state. That is to say, wait until the machine fully transitioned to the 'checking internet' state before kicking off the command that does an async call to see if the internet is there.

Once the state machine has fully transitioned to the the 'checking internet' state (its initial state actually), it kicks off the command mapped to that 'checking internet' note defined in that state's 'changed' attribute in the FSM definition, and nothing else is scheduled. We can sit there till doomsday and the machine will happily twiddle its thumbs until a StateMachine.ACTION note tells it to go to another state. In this case it will only be moved by 2 possible actions - 'check license' or 'offline hold'. The former kicks it into the 'checking license' state, the latter dumps it into a holding state that can only be transitioned out of by a 'check license' action. A timer continues to check for the internet and when it comes back online resumes progress by sending the 'check license' action.

That's it kids. The state machine officially rocks da hizouse.
-=Cliff>
« Last Edit: April 08, 2009, 05:28:23 by puremvc » Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW Email
« Reply #4 on: April 09, 2009, 12:38:31 »

It seems exactly what many of us are waiting for a long time. I'm excited to see what you've done. And no, I will not ask when will we have it as a demo here. :)
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #5 on: April 09, 2009, 06:38:17 »

You know me well. :)

But I can tell you I'm shooting for the last week of April or first week of May, when I return from FITC. This is the single most important thing, IMHO, to get out there right now. It brings sanity to the startup process that I myself have struggled with since the earliest days of Flex. And I believe that it will mesh well with other utilities such as the StartupManager or Neil's UnifiedLoader. Those utilities can do their bulk loading all within a single state, then kick the machine to the next state where more view organization happens, or whatever.

-=Cliff>
Logged
JJfutbol
Sr. Member
****
Posts: 53


View Profile WWW Email
« Reply #6 on: May 17, 2009, 08:25:27 »

I on the other hand are not so afraid of such a small man and pestering him about a release date! :) Cliff, it's already slightly past the middle of May. Any release date on that demo? Don't mean to be pushy but just as you have said the startup process is an area I've struggled with too.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #7 on: May 18, 2009, 05:56:55 »

http://seaofarrows.com/

http://seaofarrows.com/srcview

The original talk was going to center on a product I'm building but then there wasn't time and it was too involved and there's a patent application to consider, so I decided to overhaul my music site.

The code you see there has now been abstracted into a library and will shortly reappear driving http://puremvc.tv as a series of presentations, one of which describes the code itself. If I'm lucky I'll have all the recordings done by next week.

-=Cliff>
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #8 on: May 18, 2009, 06:00:06 »

BTW, for the StateMachine goodies in this demo, examine the shell.

-=Cliff>
Logged
Pages: [1]
Print