KStateMachine / kstatemachine

KStateMachine is a powerful Kotlin Multiplatform library with clean DSL syntax for creating complex state machines and statecharts driven by Kotlin Coroutines.
https://kstatemachine.github.io/kstatemachine/
Boost Software License 1.0
358 stars 21 forks source link

please add choiceDataState method #78

Closed pujunhui closed 10 months ago

pujunhui commented 10 months ago

i want do that, but the library is not support. can you implement this method? or provide other alternatives

fun main() {
    runBlocking {
        data class DoThingEvent(override val data: String) : DataEvent<String>

        val machine = createStateMachine(this, "Test") {
            val idleState = initialState("idleState")
            val doATaskState = dataState<String>("doATask")
            val doBTaskState = dataState<String>("doBTask")

            val doThingState = choiceDataState<String> {
                if (data == "doATask") {
                    doATaskState
                } else {
                    doBTaskState
                }
            }

            idleState {
                transition<DoThingEvent>(targetState = doThingState)
            }

        }
        machine.processEvent(DoThingEvent("doATask"))
    }
}
nsk90 commented 10 months ago

looks good, I dont see any blockers to implement it. I will try it out. But I suppose you will have to use dataTransition to navigate to choiceDataState

nsk90 commented 10 months ago

currently you should be able to use transitionConditionally function to archive such behaviour.

nsk90 commented 10 months ago

fixed in https://github.com/nsk90/kstatemachine/releases/tag/v0.24.0