Skip to content

Commit 29942ff

Browse files
committed
[docs] Add docs for isStartTransition extension
1 parent 8413ffa commit 29942ff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/pages/transitions/transitions.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ transition<SwitchEvent> {
9393
}
9494
```
9595

96+
## Check if the transition is triggered by StartEvent
97+
98+
When you start a StateMachine it enters it's initial state path. This is done with special
99+
library defined event called `StartEvent`.
100+
101+
Sample: There are use cases when you need to check if the state is activated by StateMachine initialization
102+
or due to some event processing in machine runtime (after initialization).
103+
104+
It can be done by checking the event type in `TransitionParams`, if it is `StartEvent` of not.
105+
The library provides convenience `TransitionParams::isStartTransition` extension property for that purpose:
106+
107+
```kotlin
108+
val machine = createStateMachine(scope) {
109+
val state1 = initialState("state1") {
110+
// will be triggered twice,
111+
// first time on initialization and the second after SwitchEvent processing
112+
onEntry {
113+
// true - if entering by StateMachine initialization (StartEvent)
114+
// false - if entering any other way, by SwitchEvent in this case.
115+
prinln(it.isStartTransition)
116+
}
117+
transitionOn<SwitchEvent> { targetState = { state2 } }
118+
}
119+
val state2 = state("state2") {
120+
transitionOn<SwitchEvent> { targetState = { state1 } }
121+
}
122+
}
123+
machine.processEvent(SwitchEvent)
124+
machine.processEvent(SwitchEvent)
125+
```
126+
96127
## Listen to all transitions in one place
97128

98129
There might be many transitions from one state to another. It is possible to listen to all of them in state machine

0 commit comments

Comments
 (0)