kstatemachine icon indicating copy to clipboard operation
kstatemachine copied to clipboard

Add history states support

Open nsk90 opened this issue 4 years ago • 3 comments

planning to add shallow history state and deep history state

nsk90 avatar Dec 11 '20 21:12 nsk90

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?

mashtonian avatar Jul 20 '22 11:07 mashtonian

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.

nsk90 avatar Jul 20 '22 19:07 nsk90

Perfect, I got it working nicely.

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

Thanks (again) for your help.

mashtonian avatar Jul 21 '22 16:07 mashtonian

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

nsk90 avatar Sep 05 '22 07:09 nsk90

issue for undo() functionality #44

nsk90 avatar Sep 07 '22 11:09 nsk90

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

nsk90 avatar Sep 14 '22 15:09 nsk90

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

mashtonian avatar Sep 14 '22 17:09 mashtonian