Skip to content

Commit

Permalink
Make state transitions respect state hierarchy more
Browse files Browse the repository at this point in the history
Previously, entering a parent state from a child state would
incorrectly fire off leave and enter events.
  • Loading branch information
bladh committed Sep 28, 2021
1 parent 1700755 commit 641be6f
Showing 1 changed file with 1 addition and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ open class SimpleStateMachine(private val initialState: KClass<*>) : Runnable {
}
}
}
currentState.leave()
stateStack.forEach { it.leave() }
}

Expand Down Expand Up @@ -71,10 +70,10 @@ open class SimpleStateMachine(private val initialState: KClass<*>) : Runnable {

private fun transition(newState: State, message: Message?) {
val newStateStack = getParentStates(newState)
newStateStack.push(newState)

// check for any potential parent states we have now left behind
if (message !is StartupMessage) {
currentState.leave()
stateStack.filter { !newStateStack.contains(it) }
.forEach { it.leave() }
}
Expand All @@ -90,7 +89,6 @@ open class SimpleStateMachine(private val initialState: KClass<*>) : Runnable {

// enter our current state
currentState = newState
enterState(newState, message)
}

private fun enterState(state: State, message: Message?) {
Expand Down

0 comments on commit 641be6f

Please sign in to comment.