From 641be6facd511ecb151a3f3289004bbab1b046a3 Mon Sep 17 00:00:00 2001 From: Alexander Bladh Date: Tue, 28 Sep 2021 20:06:54 +0200 Subject: [PATCH] Make state transitions respect state hierarchy more Previously, entering a parent state from a child state would incorrectly fire off leave and enter events. --- .../kotlin/org/tjur/simplestatemachine/SimpleStateMachine.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/kotlin/org/tjur/simplestatemachine/SimpleStateMachine.kt b/src/main/kotlin/org/tjur/simplestatemachine/SimpleStateMachine.kt index a6eabcb..3f3ce58 100644 --- a/src/main/kotlin/org/tjur/simplestatemachine/SimpleStateMachine.kt +++ b/src/main/kotlin/org/tjur/simplestatemachine/SimpleStateMachine.kt @@ -32,7 +32,6 @@ open class SimpleStateMachine(private val initialState: KClass<*>) : Runnable { } } } - currentState.leave() stateStack.forEach { it.leave() } } @@ -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() } } @@ -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?) {