Migration guide
Page contents
Migrating from versions older than v0.20.0
v0.20.0 introduced built-in coroutines support and changed how state machines are created. All public APIs gained suspend modifiers; blocking counterparts (with a Blocking suffix) were added for non-coroutine contexts.
If you already have or are ready to add a Kotlin Coroutines dependency
- Add both
kstatemachineandkstatemachine-coroutinesto your build. - Replace machine creation with
createStateMachineorcreateStateMachineBlockingfromkstatemachine-coroutines, passing aCoroutineScopeas the first argument. - Prefer suspendable function variants (
start,stop,processEvent,undo, etc.) whenever you are inside a coroutine. - Avoid the
Blockingsuffix variants, especially recursively (e.g. from inside a listener callback) — they userunBlockinginternally and will deadlock in that context.
See Multithreading for the single-threaded CoroutineScope requirement and the CoroutineContext preservation guarantee.
If you cannot or do not want to depend on Kotlin Coroutines
- Use only the
kstatemachineartifact. - Replace machine creation with
createStdLibStateMachine. - Inside machine callbacks you can still use suspendable function variants —
suspendis a compiler feature, not a Coroutines library feature, so it works without the library on the classpath. - Outside callbacks, use the
Blockingsuffix variants; this is safe in a non-coroutine context. - Do not call Kotlin Coroutines APIs from a machine created by
createStdLibStateMachine— an exception will be thrown.