Using FSM to control app startup

Joni's Avatar

Joni

16 May, 2010 03:14 PM

Hello, I'm using the FSM to control my app startup sequence.
I have defined all of my constants regarding the StateMachine in one class.
In one of my startup states I call a service to load an external XML, and I have to wait for this XML to load before proceeding to the next state.
I could dispatch a StateEvent.ACTION from within my service to proceed to the next state, but this kind of bugs me since I'm coupling my service with this specific FSM.
I could also dispatch an event specific to this service, lets say XMLServiceEvent.LOADED, which would be mapped to a command XMLLoadedCommand, which would in turn dispatch the StateEvent.ACTION to proceed to the next state.. But isn't this overkill?

So... My question is: What is the best way to tell the StateMachine to proceed to the next state once the XML has been loaded?

Thanks!

  1. 2 Posted by Jonny Reeves on 17 May, 2010 12:40 PM

    Jonny Reeves's Avatar

    Hi Joni,

    You are right to not want to couple your Services to the FSM implementation as this would make them difficult to re-use in other projects. Your Service should always dispatch a Service-specific event (ie: ConfigServiceEvent.CONFIG_LOADED) which the framework can pickup on and react to.

    Personally, I don't think there's anything wrong with listening for the Service success event and mapping to to a Command which triggers a State change as that's part of your wider application flow.

    However, if you do feel this is too much glue, you could reduce it by simply listening for the Service's success event using a Function delegate, ie:

    function execute() : void {
        service.eventDispatcher.addEventListener(ServiceEvent.SUCCESS, onSuccessEvent);
    }
    
    function onSuccessEvent(event : ServiceEvent) : void {
        dispatch(new StateEvent(StateEvent.ACTION, StateMap.NEW_STATE));
    }
    

    Hope that helps
    Jonny.

  2. 3 Posted by Joni on 17 May, 2010 02:46 PM

    Joni's Avatar

    Where would I set up that function delegate? In the same Command that I use the service to load the XML?

    Thanks

  3. 4 Posted by Jonny Reeves on 17 May, 2010 03:22 PM

    Jonny Reeves's Avatar

    Hi Joni,

    Yep, in the same command where you use the Service. As long as your use an Event Listener with a strong reference (the default) then the Command will not be garbage collected.

    Just don't forget to remove the Event Listener when you handle the successEvent (otherwise the Service, and therefore the command will never be gc'd)

    Jonny.

  4. 5 Posted by jce on 03 Feb, 2011 05:56 PM

    jce's Avatar

    i had these same questions last year and somehow never found this! (i know its old now, but) for future reference, there are some more strategies discussed in this thread:
    http://knowledge.robotlegs.org/discussions/questions/345-fsm-async-...

    cheers

  5. Stray closed this discussion on 13 Feb, 2011 04:18 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac