StateMachine icon indicating copy to clipboard operation
StateMachine copied to clipboard

Allow split state definitions

Open SpaceBison opened this issue 5 years ago • 3 comments

Consider the following state machine:

StateMachine.create<String, Int, Nothing> {
	initialState(STATE_A)

	state(STATE_A) {
		on(EVENT_1) {
			transitionTo(STATE_B)
		}

		onExit(firstDefinitionOnExitListener)
	}

	state(STATE_A) {
		on(EVENT_2) {
			transitionTo(STATE_B)
		}

		onExit(secondDefinitionOnExitListener)
	}

	state(STATE_B) {
		onEnter(firstDefinitionOnEnterListener)
	}

	state(STATE_B) {
		onEnter(secondDefinitionOnEnterListener)
	}
}

Currently, trying to transition from STATE_A on EVENT_1 will result in a valid transition, but attempting a transition on EVENT_2 will result in an invalid transition. There's no warning or error given neither for the second STATE_A nor the second STATE_B definitions. A similar behavior can be observed for on exit and on enter listeners - only firstDefinitionOnExitListener and firstDefinitionOnEnterListener will be called when transitioning from STATE_A to STATE_B.

This PR aims to make state machines take into account all the state definitions passed to the graph builder. This is achieved by merging all applicable state definitions when resolving a transition for a given state and event instead of just taking the first one that matches.

SpaceBison avatar Jun 03 '20 12:06 SpaceBison

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 03 '20 12:06 CLAassistant

This would be great.

// I can handle an event in one place  for more than one state.
  state(StateMachine.Matcher.any<State, State>().where {
            !(this is State.MachineError)
        })

timzaak avatar Jun 30 '20 04:06 timzaak

What do we need for this PR to be merged and have a version with this change?

JaviRpo avatar Jul 01 '21 19:07 JaviRpo