@@ -334,17 +334,31 @@ createStateMachine {
334
334
335
335
## Finishing states and state machine
336
336
337
- Some of state machines are infinite, but other ones may finish. State machine that was finished stops processing
338
- incoming events. To make state machine finishing, add ` FinalState ` to it with ` finalState() ` function or add any
339
- subclass of ` FinalState ` with ` addFinalState() ` function. In ` ChildMode.EXCLUSIVE ` state machine finishes when enters
340
- top-level ` FinalState ` . In ` ChildMode.PARALLEL ` state machine finishes when all its children has finished.
337
+ Some of state machines and states are infinite, but other ones may finish.
341
338
342
- Composite states also may have ` FinalState ` children. Whole state machine is not finished in this case and transitions
343
- of finished states are still active. But notification is triggered and ` isFinished ` property set.
339
+ * In ` ChildMode.EXCLUSIVE ` state or state machine finishes when enters top-level final state.
340
+ * In ` ChildMode.PARALLEL ` state or state machine finishes when all its children has finished.
341
+
342
+ To make a state final, it must implement ` FinalState ` marker interface.
343
+ Built-in implementation of such state is ` DefaultFinalState ` .
344
+ It can be created directly with ` finalState() ` function or be subclassed and added with ` addFinalState() ` function.
345
+ Alternatively you can inherit basic ` DefaultState ` and mark it with ` FinalState ` explicitly like here:
346
+
347
+ ``` kotlin
348
+ sealed class States : DefaultState () {
349
+ object State1 : States()
350
+ object State2 : States(), FinalState
351
+ }
352
+ ```
353
+
354
+ Finishing of states and state machines is treated little differently.
355
+ State machine that was finished stops processing incoming events.
356
+ But when some nested state is finished its transitions are still active,
357
+ only notification is triggered and ` isFinished ` property set.
344
358
345
359
Notifications about finishing are available in two forms:
346
360
347
- 1 ) Triggering of ` onFinished() ` listener callback. This is the only option for StateMachines .
361
+ 1 ) Triggering of ` onFinished() ` listener callback. This is the only option for ` StateMachine ` .
348
362
``` kotlin
349
363
val machine = createStateMachine {
350
364
val final = finalState(" final" )
@@ -354,10 +368,9 @@ Notifications about finishing are available in two forms:
354
368
}
355
369
machine.isFinished // is true
356
370
```
357
- 2 ) Generation and processing of special `FinishedEvent `. This option is also available for composite states and useful
358
- for performing transitions on finishing. Transitions for `FinishedEvent ` are matched with this event in special way,
359
- so such transition is triggered only for `FinishedEvent ` generated by finishing of a state that defines such
360
- transition, not another one.
371
+ 2 ) Generation and processing of special `FinishedEvent `. This option is also available for composite states and useful
372
+ for performing transitions on finishing:
373
+
361
374
```kotlin
362
375
createStateMachine {
363
376
val state2 = state(" state2" )
@@ -371,6 +384,9 @@ Notifications about finishing are available in two forms:
371
384
}
372
385
}
373
386
```
387
+ Transition for `FinishedEvent ` is detected by the library and matched by special kind of `EventMatcher `,
388
+ so such transition is triggered only for `FinishedEvent ` that corresponds to this state.
389
+ `FinishingEvent ` generated by finishing of another state will not trigger such transition.
374
390
375
391
## Nested states
376
392
0 commit comments