KStateMachine / kstatemachine

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

data transition should no be self targeted, use simple transition instead #77

Closed pujunhui closed 7 months ago

pujunhui commented 7 months ago

in my case, i want to re entry the same DataState, in the state i what to do some things by data. but the library not allow i do that. Please tell me how to achieve my needs.

fun main() {
    runBlocking {
        val machine = createStateMachine(this, "Test") {
            val idleState = initialState("IdleState")
            val runState = dataState<DataInfo>("RunState")
            idleState {
                dataTransition<RunEvent, DataInfo> {
                    targetState = runState
                }
            }
            runState {
                var job: Job? = null
                onEntry {
                    log { "onEntry $name $data ${Thread.currentThread().name}" }
                    job = launch(Dispatchers.IO) {
                        var i = 0
                        while (isActive) {
                            log { "running ${i++} $data ${Thread.currentThread().name}" }
                            delay(1000)
                        }
                    }
                }
                onExit {
                    job?.cancel()
                }

                dataTransition<RunEvent, DataInfo> {
                    type = TransitionType.EXTERNAL
                    targetState = runState
                }
            }
        }
        machine.processEvent(RunEvent(DataInfo("Noting")))
        delay(10000)
        machine.processEvent(RunEvent(DataInfo("Do Something")))
    }
}
nsk90 commented 7 months ago

Hi, which library version do you use? The ability to use self targeted data transition was fixed in a last version. https://github.com/nsk90/kstatemachine/releases/tag/v0.23.0

pujunhui commented 7 months ago

Hi, which library version do you use? The ability to use self targeted data transition was fixed in a last version. https://github.com/nsk90/kstatemachine/releases/tag/v0.23.0

sorry,i use library version is v0.22.0(because your home page is v0.22.0 ). i resolved the problem after update version to v0.23.0.