Skip to content

Commit a27cc9f

Browse files
committed
[docs] Update docs regarding FinishingEvent
1 parent 5324f1a commit a27cc9f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/index.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,31 @@ createStateMachine {
334334

335335
## Finishing states and state machine
336336

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.
341338

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.
344358

345359
Notifications about finishing are available in two forms:
346360

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`.
348362
```kotlin
349363
val machine = createStateMachine {
350364
val final = finalState("final")
@@ -354,10 +368,9 @@ Notifications about finishing are available in two forms:
354368
}
355369
machine.isFinished // is true
356370
```
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+
361374
```kotlin
362375
createStateMachine {
363376
val state2 = state("state2")
@@ -371,6 +384,9 @@ Notifications about finishing are available in two forms:
371384
}
372385
}
373386
```
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.
374390

375391
## Nested states
376392

0 commit comments

Comments
 (0)