5656 * [ Controlling export output] ( #controlling-export-output )
5757* [ Persistence] ( #persistence )
5858 * [ Event recording] ( #event-recording )
59+ * [ Restoring StateMachine] ( #restoring-statemachine )
5960* [ Testing] ( #testing )
6061* [ Multiplatform] ( #multiplatform )
6162* [ Consider using Kotlin sealed classes] ( #consider-using-kotlin-sealed-classes )
@@ -938,7 +939,7 @@ that `volatile` keyword provides on `jvm`.
938939
939940Suspendable functions and their `Blocking` analogs internally switch current execution `СoroutineСontext`
940941(from which they are called) to state machines one, using `kotlinx.coroutines.withContext` or
941- `kotlinx.coroutines.runBlocking` arguments respectively.
942+ `kotlinx.coroutines.runBlocking` functions respectively.
942943This is `CoroutineContext` preservation guarantee that the library provides.
943944Note that if you created machine with the scope containing `kotlinx.coroutines.EmptyCoroutineContext` switching will not
944945be performed. So if the StateMachine is created with correct (meeting above conditions) scope it is safe to call
@@ -1089,11 +1090,11 @@ See [PlantUML with MetaInfo export sample](https://github.com/nsk90/kstatemachin
10891090
10901091## Persistence
10911092
1092- **Persist** `StateMachine` - means transform it into serializable representation, such as `Serializable` object or
1093- JSON text, and possibly saving it into some persistent storage like file or sending by network.
1094- ** Restoration** is a process of restoring the `StateMachine` from the serializable representation.
1093+ * * *Persist** `StateMachine` - means transform it into serializable representation, such as `Serializable` object or
1094+ JSON text, and possibly saving it into some persistent storage like file or sending by network.
1095+ * ** Restoration** - is a process of restoring the `StateMachine` from the serializable representation.
10951096
1096- There are several kinds or levels of `StateMachine` persistence (serialization). Let' s look at sample use cases:
1097+ There are several kinds or levels of `StateMachine` persistence (serialization). Let' s look at sample use cases:
10971098
109810991 ) ** Structure + configuration** - Create `StateMachine ` on some process/ host and send its structure and
10991100 active configuration by network to another process/ host.
@@ -1113,7 +1114,44 @@ Case 2 in turn may be reached in two different ways:
11131114
11141115### Event recording
11151116
1116- WIP
1117+ The library supports event recording out of the box. To enable it you should use `EventRecordingArguments ` in
1118+ `CreationArguments ` when creating a machine instance by `createStateMachine()` functions family. The recording process
1119+ can be configured with `EventRecordingArguments ` properties.
1120+
1121+ ```kotlin
1122+ val machine = createStateMachine(
1123+ creationArguments = CreationArguments (eventRecordingArguments = EventRecordingArguments ())
1124+ ) {
1125+ // ...
1126+ }
1127+ ```
1128+
1129+ When the machine had processed necessary events, and you want to save its state configuration, first you have to
1130+ get the recorded events:
1131+
1132+ ``` kotlin
1133+ val recordedEvents = machine.eventRecorder.getRecordedEvents()
1134+ ```
1135+
1136+ ` RecordedEvents ` object now is ready to be serialized. Currently, the library does not provide an implementation
1137+ of serialization process, so it is up to user to write serialization code. The serialization support is planned
1138+ using ` kotlinx.serialization ` library in further ` KStateMachine ` versions.
1139+
1140+ ### Restoring StateMachine
1141+
1142+ When a user wants to restore the StateMachine, he deserializes ` RecordedEvents ` object and
1143+ creates StateMachine instance having exactly the same structure as original one.
1144+ Typically, both instances are created by the same code.
1145+
1146+ Calling ` restoreByRecordedEvents() ` or its blocking analog ` restoreByRecordedEventsBlocking() ` will process
1147+ recorded events over just created StateMachine instance.
1148+
1149+ ``` kotlin
1150+ machine.restoreByRecordedEvents(recordedEvents)
1151+ ```
1152+
1153+ ` restoreByRecordedEvents() ` method will start the machine if necessary.
1154+ You can configure restoration process by ` restoreByRecordedEvents() ` arguments.
11171155
11181156## Testing
11191157
0 commit comments