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]
Author Topic: FSM Utility Question  (Read 3323 times)
Jr. Member
Posts: 13

View Profile Email
« on: December 18, 2008, 10:26:29 »

Hi guys,

I just started using the FMS utility, wich I think it´s a incredible utility, and I already got some questions.

I´m making a simple remote login app. The FMS xml is as follow

<fsm initial={App.STATE_READY}>
  <state name={App.STATE_READY} entering={LoginPanelMediator.RESET} exiting= LoginPanelMediator.LOGIN_TRY}>
    <transition action={LoginPanelMediator.VALID_LOGIN_FORM} target={App.STATE_TRY_LOGIN}/>
  <state name={App.STATE_TRY_LOGIN} entering={ApplicationFacade.CMD_LOGIN}>
    <transition action={UserProxy.ACTION_LOGIN_OK} target={App.STATE_USER_HOME}/>
    <transition action={UserProxy.ACTION_LOGIN_FAIL} target={App.STATE_READY}/>
  <state name={App.STATE_USER_HOME} >
    <transition action={App.ACTION_LOGOUT} target={App.STATE_READY}/>

The idea is that simple: Initial state presents the Login Panel. When the user click on button "Log me in!" the app changes to the "Trying to Login" State that shows a wait message. The login is validated on a PHP remote server. If the response is a valid login, change the app state to the "User Home State" wich shows the user´s home screen. If not valid login, goes back to initial state showing the error message sent by the server. The only action that is avaiable on User Home state is the Logout wich brings the user back to the initial state of the app.

Well, that didn´t work as I expected. In fact, I found my self traped in a mase of notifications and actions and commands. And the timming and flow of them.

The problem was that the remote response was being faster that the FSM could change state. In detail: the UserProxy executes the login() method, the response is instantaneous, so it send the UserProxy.ACTION_LOGIN_* notification. But, the app is still in STATE_READY that doesn´t know how to process the action, so the app stops working.

To fix that, I just changed the StateMachine.as, transitionTo() method like this:
// Enter the next State
currentState = nextState;
if ( nextState.entering ) sendNotification( nextState.entering, nextState );
//currentState = nextState;

I don´t know if this right but ... what do you guys think? I really don´t feel confortable changing the Utilitys source but other fixes would need much more coding.

Looking foward for you solutions and comments.  :)
Global Moderator
Hero Member
Posts: 2871

View Profile WWW Email
« Reply #1 on: December 18, 2008, 01:13:39 »

Changing the state before the exiting and entering notifications have been sent is not valid. For instance, until you enter a room, you cannot be considered to be in that room, only entering it.

Though it may be fast, the response is still asynchronous, and thus once made, the thread of execution will return immediately to the StateMachine's next line, where the current state is set to the new state.

And since Flash is single threaded, the service return should not be tended until the current thread has executed your synchronous code and  reached the player's main loop.

I would suggest using the debugger to follow the thread of execution step by step.

Pages: [1]