Skip to content

Commit 80e942a

Browse files
committed
[docs] Add kstatemachine-coroutines artifact documentation
1 parent 1902656 commit 80e942a

5 files changed

Lines changed: 85 additions & 18 deletions

File tree

docs/pages/coroutines_artifact.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.

docs/pages/events.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ Generally it is not recommended to confuse them with commands, see [do not secti
2424
When a StateMachine is created, configured and started it is ready to process incoming events.
2525
It is done with `processEvent()` functions family.
2626

27-
* `processEvent()` - suspendable version
28-
* `processEventBlocking()` - blocking version. Not suspendable, uses `kotlinx.coroutines.runBlocking`
27+
* `processEvent()` - suspendable version, synchronous
28+
* `processEventBlocking()` - blocking version. Not suspendable, synchronous. Uses `kotlinx.coroutines.runBlocking`
2929
internally if you use StateMachine with coroutines support. Or just runs code in-place for StdLib StateMachine
3030
instance.
31-
* `processEventByLaunch()` - (available in `kstatemachine-coroutines` artifact) Not suspendable, uses StateMachine's
32-
`CouroutineScope` to process event in a new coroutine by `kotlinx.coroutines.launch` function.
31+
* `processEventByLaunch()` - (available in `kstatemachine-coroutines` artifact) Not suspendable, asynchronous, uses
32+
StateMachine's `CouroutineScope` to process event in a new coroutine by `kotlinx.coroutines.launch` function.
3333
Cannot be used with StdLib StateMachine instance (throws in this case).
34-
* `processEventByAsync()` - (available in `kstatemachine-coroutines` artifact) Not suspendable, uses StateMachine's
35-
`CouroutineScope` to process event in a new coroutine by `kotlinx.coroutines.async` function.
34+
* `processEventByAsync()` - (available in `kstatemachine-coroutines` artifact) Not suspendable, asynchronous, uses
35+
StateMachine's `CouroutineScope` to process event in a new coroutine by `kotlinx.coroutines.async` function.
3636
Returns`kotlinx.coroutines.Deffered` with `ProcessingResult`.
3737
Cannot be used with StdLib StateMachine instance (throws in this case).
3838

docs/pages/multithreading.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Many functions like `createStateMachine`/`start`/`stop`/`processEvent`/`undo` et
2828
has analogs with `Blocking` suffix which are not marked with `suspend` keyword.
2929
If you use KStateMachine with coroutines support you should prefer suspendable function versions.
3030
Note that `Blocking` versions internally use `kotlinx.coroutines.runBlocking` function which is rather dangerous and
31-
may cause deadlocks if used not properly. That is why you should avoid using `Blocking` APIs from coroutines and
32-
recursively (from library callbacks).
31+
may cause deadlocks if used not properly (especially recursively).
32+
That is why you should avoid using `Blocking` APIs from coroutines and recursively (from library callbacks).
3333

3434
### Use single threaded `CoroutineScope`
3535

@@ -110,14 +110,6 @@ runBlocking { // defines non-empty coroutine context for state machine
110110
}
111111
```
112112

113-
## Additional kstatemachine-coroutines artifact
114-
115-
Contains additional functions to work with KStateMachine depending on Kotlin Coroutines library
116-
117-
* `createStateMachine()` / `createStateMachineBlocking()` creates state machine with specified `CoroutineScope`
118-
* `stateMachineNotificationFlow()` returns a `SharedFlow` of all machine notifications
119-
* `activeStatesFlow()` returns a `StateFlow` of active machine states
120-
121113
## Migration guide from versions older than v0.20.0
122114

123115
### If you already have or ready to add Kotlin Coroutines dependency

docs/pages/statemachine.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ nav_order: 1
1717

1818
A state machine can be created with one of those factory functions:
1919

20-
* `createStateMachine()` suspendable version (from `kstatemachine-coroutines` artifact)
20+
* `createStateMachine()` suspendable version (from `kstatemachine-coroutines` artifact), the best choice by default.
2121
* `createStateMachineBlocking()` blocking version (from `kstatemachine-coroutines` artifact)
2222
* `createStdLibStateMachine()` - creates StateMachine instance without Kotlin Coroutines support
2323
(from `kstatemachine` artifact)

tests/src/commonTest/kotlin/ru/nsk/kstatemachine/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fun createTestStateMachine(
116116
init = init
117117
)
118118
CoroutineStarterType.COROUTINES_LIB_SINGLE_THREAD_DISPATCHER -> createStateMachineBlocking(
119-
CoroutineScope(newSingleThreadContext("test single thread context")),
119+
CoroutineScope(newSingleThreadContext("test single thread context")), // fixme context leaks
120120
name,
121121
childMode,
122122
start,

0 commit comments

Comments
 (0)