StateMachine changed - possible to add data?

Philip's Avatar

Philip

12 Mar, 2011 12:03 PM via web

Hi there,

I'm using Joel Hooks' "robotlegs-utilities-StateMachine", and was wondering if it's possible to populate the data property of the StateEvent that is dispatched when the <state changed="myEvent"> is fired. A non-working example is to be able to do something similar to the following:

<state name={ HOME } changed={ NavEvent.GO, NavIDEnum.HOME }>

which would make the resulting StateEvent contain { type:NavEvent.GO, data:NavIDEnum.HOME }

This would allow me to create one command mapping:

commandMap.mapEvent( NavEvent.GO, NavCommand, StateEvent );

and my NavCommand could check the StateEvent.data property and figure out that in this case we need to navigate to NavIDEnum.HOME.

Thanks,
Phil

  1. Support Staff 2 Posted by Joel Hooks on 12 Mar, 2011 03:21 PM

    Joel Hooks's Avatar

    Did you try it? My gut tells me that it won't work out of the box as you expect above.

  2. 3 Posted by neil on 12 Mar, 2011 04:11 PM

    neil's Avatar

    the changed attribute will just be stringified, and dispatched as the event type.
    It shouldn't be to hard to extend / re-write to do as you wish, though.

  3. 4 Posted by Philip Bulley on 12 Mar, 2011 05:14 PM

    Philip Bulley's Avatar

    Thanks guys! So I peeked into the code, and that makes sense.

    I figured that instead of passing a second parameter through as StateEvent.data, it may be a better idea to use StateEvent.action, as that is always set to null. What are your thoughts on that?

    I've made a very small modification so I can setup my state as:

    <state name={ State.HOME } changed={ [NavEvent.GO, NavIDEnum.HOME] } >
    

    this assigns NavEvent.GO to StateEvent.type and NavIDEnum.HOME to StateEvent.action.

    I changed (in StateMachine.as):

    eventDispatcher.dispatchEvent( new StateEvent( _currentState.changed, null, data ));
    

    to

    eventDispatcher.dispatchEvent( new StateEvent( _currentState.changed.split(',')[0], _currentState.changed.split(',')[1], data ));
    

    If a second changed parameter is not specified, or changed is not defined as an array, StateEvent.action is set to undefined (instead of previously always being null), so should be backwards compatible.

    I guess the same could easily be done for exiting and entering.

    What are your thoughts?

  4. 5 Posted by neil on 12 Mar, 2011 05:32 PM

    neil's Avatar

    err, does it work?
    sorry, my head has been in a very different code base

  5. 6 Posted by neil on 12 Mar, 2011 05:48 PM

    neil's Avatar

    Actually, best not use Event.action, as it is used for telling the FSM to transition. I would add another property - info, or something like that. That would allow you to still pass proper payloads in the data property

  6. 7 Posted by Philip Bulley on 12 Mar, 2011 05:52 PM

    Philip Bulley's Avatar

    Yep it works :) It solves my scenario.

    Seeing as it's such a small change, but adds a nice bit of functionality, do you think it could be something useful for the main codebase?

    The very slightly modified StateMachine.as is here (http://pastie.org/1663935) if you fancy trying it.

  7. 8 Posted by Philip Bulley on 12 Mar, 2011 05:55 PM

    Philip Bulley's Avatar

    I was pondering that regarding StateEvent.action, but thought that as it's never used during StateEvent.CHANGED, I could use it to define a custom action. Or is that confusing?

  8. 9 Posted by neil on 12 Mar, 2011 06:12 PM

    neil's Avatar

    I think that using the action would be confusing as its name is specific, and your use is against its purpose.

    I would get ( if you haven't already ) a github account and fork joel's repo https://github.com/joelhooks/robotlegs-utilities-StateMachine.

    then it will be in the public domain and easy to find

  9. 10 Posted by Philip Bulley on 12 Mar, 2011 07:06 PM

    Philip Bulley's Avatar

    Will do. Gotta pull myself away form this now, so I'll put it up there tomorrow. For reference it'll be here: https://github.com/milkisevil/

    BTW, I'll go with StateEvent.info :)

    Thanks for your help guys!

  10. 11 Posted by neil on 12 Mar, 2011 10:16 PM

    neil's Avatar

    no probs :)

  11. Stray closed this discussion on 02 Apr, 2011 12:00 PM.

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