|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Coroutines artifact |
| 4 | +--- |
| 5 | + |
| 6 | +# Coroutines artifact |
| 7 | + |
| 8 | +{: .no_toc } |
| 9 | + |
| 10 | +## Page contents |
| 11 | + |
| 12 | +{: .no_toc .text-delta } |
| 13 | + |
| 14 | +- TOC |
| 15 | + {:toc} |
| 16 | + |
| 17 | +This page contains information about `kstatemachine-coroutines` artifact functionality. |
| 18 | +Which contains the library APIs for working in `Kotlin Coroutines` environment. |
| 19 | + |
| 20 | +> This artifact depends on Kotlin Coroutines library |
| 21 | +
|
| 22 | +You can find common information about multithreading library usage and coroutines on |
| 23 | +[multithreading page](https://kstatemachine.github.io/kstatemachine/pages/multithreading.html) |
| 24 | + |
| 25 | +## Artifact separation |
| 26 | + |
| 27 | +`KStateMachine` has first class support of coroutines. Even if you don't use `Kotlin Coroutines` |
| 28 | +and `kstatemachine-coroutines` artifact all library callbacks are `suspendable` functions. |
| 29 | +So the functionality of this module should not be treated as "wrappers" or "extensions". |
| 30 | +This is just a core functionality which is separated from original `kstatemachine` artifact |
| 31 | +to fallow language architecture regarding coroutines support. |
| 32 | + |
| 33 | +Contains additional functions to work with KStateMachine depending on Kotlin Coroutines library |
| 34 | + |
| 35 | +## State machine creation |
| 36 | + |
| 37 | +The artifact contains `createStateMachine()` / `createStateMachineBlocking()` methods, which were described in |
| 38 | +[Create state machine](https://kstatemachine.github.io/kstatemachine/pages/statemachine.html#create-state-machine) |
| 39 | +block |
| 40 | + |
| 41 | +## Flow notifications |
| 42 | + |
| 43 | +Coroutines users often use `Flow` to get some changes from a source. |
| 44 | +The library provides `StateMachine` extension methods which represents notification APIs (listeners) in a form of `Flow`: |
| 45 | + |
| 46 | +* `stateMachineNotificationFlow()` returns a `SharedFlow` of all machine notifications: |
| 47 | + |
| 48 | +```kotlin |
| 49 | + machine.stateMachineNotificationFlow().collect { |
| 50 | + when (it) { |
| 51 | + is Started -> println("Started ${it.machine}") |
| 52 | + is TransitionTriggered -> println("TransitionTriggered ${it.transitionParams.event}") |
| 53 | + is TransitionComplete -> println("TransitionComplete ${it.transitionParams.event}") |
| 54 | + is StateEntry -> println("StateEntry ${it.state}") |
| 55 | + is StateExit -> println("StateExit ${it.state}") |
| 56 | + is StateFinished -> println("StateFinished ${it.state}") |
| 57 | + is Stopped -> println("Stopped ${it.machine}") |
| 58 | + is Destroyed -> println("Destroyed ${it.machine}") |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +* `activeStatesFlow()` returns a `StateFlow` of active machine states: |
| 64 | + |
| 65 | +```kotlin |
| 66 | +machine.activeStatesFlow().collect { activeStates -> |
| 67 | + println("The set of active states: $activeStates") |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Event processing |
| 72 | + |
| 73 | +`processEventByLaunch()` and `processEventByAsync()` functions are described in |
| 74 | +[event processing](https://kstatemachine.github.io/kstatemachine/pages/events.html#event-processing) block. |
| 75 | +You can use them to process events in asynchronous way. |
0 commit comments