KStateMachine / kstatemachine

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.
https://kstatemachine.github.io/kstatemachine/
Boost Software License 1.0
340 stars 19 forks source link

Add history states support #14

Closed nsk90 closed 1 year ago

nsk90 commented 3 years ago

planning to add shallow history state and deep history state

mashtonian commented 1 year ago

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?

nsk90 commented 1 year ago

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.

mashtonian commented 1 year ago

Perfect, I got it working nicely.

I'll look out for a future release with it built in!

Thanks (again) for your help.

nsk90 commented 1 year ago

History state added in https://github.com/nsk90/kstatemachine/releases/tag/v0.14.0

nsk90 commented 1 year ago

issue for undo() functionality #44

nsk90 commented 1 year ago

@mashtonian you can try undo functionality in https://github.com/nsk90/kstatemachine/releases/tag/v0.15.0 =)

mashtonian commented 1 year ago

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: @.***>