kstatemachine
kstatemachine copied to clipboard
Add history states support
planning to add shallow history state and deep history state
Hi,
I'm interested in this as I am trying to implement an "Undo" function that allows the machine to go "backwards" through previous states.
As I exit each state, I store it in an undoList, then when processing the Undo event, I pass that state as an argument and attempt to set targetState
to it, in the onTriggered
listener of the Undo event:
when (shot) {
is UNDO -> {
val undoState: State? = undoList.removeLastOrNull()
if (undoState != null) {
stateMachine.processEvent(shot, argument = undoState)
}
}
else -> stateMachine.processEvent(shot)
}
...
private val stateMachine = createStateMachine {
onTransition {
if (it.transition.sourceState.name != null) {
println("Exiting state ${it.transition.sourceState.name}")
println("Entering state ${it.direction.targetState?.name}")
if (it.event !is UNDO)
undoList.add(it.transition.sourceState as State)
}
}
transition<UNDO> {
onTriggered {
targetState = it.argument as State
removeLastShot()
}
}
...
Unfortunately this doesn't work.
I also can't set the targetState in it.direction
, as that is immutable.
Can you advise if this is possible?
Try using conditional transition which allows to calculate target state in "runtime". https://github.com/nsk90/kstatemachine/wiki#conditional-transitions
I am planning to implement this possibility, but it is not finished yet, and there is still a lot of work to do until complete.
Perfect, I got it working nicely.
I'll look out for a future release with it built in!
Thanks (again) for your help.
History state added in https://github.com/nsk90/kstatemachine/releases/tag/v0.14.0
issue for undo() functionality #44
@mashtonian you can try undo functionality in https://github.com/nsk90/kstatemachine/releases/tag/v0.15.0 =)
Oooh, nice.
I'll have a go later this week.
Thanks!
On Wed, 14 Sept 2022, 16:43 Mikhail Fedotov, @.***> wrote:
@mashtonian https://github.com/mashtonian you can try undo functionality in https://github.com/nsk90/kstatemachine/releases/tag/v0.15.0 =)
— Reply to this email directly, view it on GitHub https://github.com/nsk90/kstatemachine/issues/14#issuecomment-1246960995, or unsubscribe https://github.com/notifications/unsubscribe-auth/APZQI4ZIGNP6WCMAMTVUHATV6HXDPANCNFSM4UXHU74Q . You are receiving this because you were mentioned.Message ID: @.***>