Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnEnter not called for initialState #35

Open
dariush-fathie opened this issue Apr 11, 2021 · 1 comment
Open

OnEnter not called for initialState #35

dariush-fathie opened this issue Apr 11, 2021 · 1 comment

Comments

@dariush-fathie
Copy link

dariush-fathie commented Apr 11, 2021

Thank you for your amazing library
I have a problem with onEnter at initialState.

It's my code :

   val machine = StateMachine.create<State, Event, SideEffect> {
   initialState(State.INIT)
   
   state<State.INIT> {
      onEnter {
         println("onEnter INIT")
      }
      
      on<Event.E1> {
         println("onE1 INIT")
         transitionTo(State.INITIALIZED)
      }
   }
   
   state<State.INITIALIZED> {
      onEnter {
         println("enter initialized")
      }
      
      on<Event.E1> {
         transitionTo(State.FINAL)
      }
      
      onExit {
         println("exit initialized")
      }
   }
   
   state<State.FINAL> {
      onEnter {
         println("onFinal")
      }
   }
   
   
}


machine.transition(Event.E1)
machine.transition(Event.E1)

sealed class State {
   object INIT : State()
   object INITIALIZED : State()
   object FINAL : State()
}

sealed class Event {
   object E1 : Event()
}

sealed class SideEffect {
   
}

Result:

onE1 INIT
enter initialized
exit initialized
onFinal

@dtbullock
Copy link

I wouldn't expect an 'initial' state to be entered? That would imply that it is transitioning from some other state, and that wouldn't make it the initial state. So maybe you just need an extra state to be the 'initial nothing state'?

Eg. My app's state-machine starts in a 'Disconnected' state. It doesn't take a transition to get there ... that's the correct state for the state-machine to be in once it is built (it has to be in some state already!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants