Skip to content

Commit 35f05c0

Browse files
committed
Remove onEvent action lambda argument
1 parent 3a1ab3a commit 35f05c0

3 files changed

Lines changed: 7 additions & 29 deletions

File tree

lib/src/main/kotlin/io/nexure/fsm/StateMachine.kt

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,3 @@ interface StateMachine<S : Any, E : Any> {
3939
fun <S : Any, E : Any> builder(): StateMachineBuilder.Uninitialized<S, E> = StateMachineBuilder()
4040
}
4141
}
42-
43-
/**
44-
* Execute a transition from [state] to another state depending on [event].
45-
* Returns a [Transition] indicating if the transition was permitted and
46-
* successful or not by the state machine.
47-
*
48-
* It is recommended that the return value is checked for the desired outcome, if it is critical
49-
* that an event for example is accepted and not rejected.
50-
*
51-
* This extension method is just syntactic sugar for calling
52-
* ```kotlin
53-
* stateMachine.onEvent(currentState, event).onTransition { newState ->
54-
* // Do something
55-
* }
56-
* ```
57-
*/
58-
inline fun <S : Any, E : Any> StateMachine<S, E>.onEvent(
59-
state: S,
60-
event: E,
61-
action: (state: S) -> Unit
62-
): Transition<S> = onEvent(state, event).onTransition { action(it) }
63-

lib/src/test/kotlin/io/nexure/fsm/ExampleStateMachine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun callExampleStateMachine(fsm: StateMachine<PaymentState, PaymentEvent>) {
3636
assertEquals(Accepted(PaymentState.Pending), state1)
3737

3838
// Transition from state PENDING into state AUTHORIZED
39-
val state2 = fsm.onEvent(PaymentState.Pending, PaymentEvent.BankAuthorization) {
39+
val state2 = fsm.onEvent(PaymentState.Pending, PaymentEvent.BankAuthorization).onTransition {
4040
// Invoke some optional action when payment was authorized
4141
}
4242
assertEquals(Accepted(PaymentState.Authorized), state2)

lib/src/test/kotlin/io/nexure/fsm/StateMachineTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class StateMachineTest {
6363
.connect('b', 'c', Event.E3)
6464
.build()
6565

66-
assertEquals(Accepted('b'), fsm.onEvent('a', Event.E2) { n += 2})
67-
assertEquals(Accepted('c'), fsm.onEvent('b', Event.E3) { n += 3})
66+
assertEquals(Accepted('b'), fsm.onEvent('a', Event.E2).onTransition { n += 2})
67+
assertEquals(Accepted('c'), fsm.onEvent('b', Event.E3).onTransition { n += 3})
6868

6969
assertEquals(5, n)
7070
}
@@ -140,7 +140,7 @@ class StateMachineTest {
140140
.connect(State.S1, State.S2, Event.E1)
141141
.build()
142142

143-
fsm.onEvent(State.S1, Event.E1){
143+
fsm.onEvent(State.S1, Event.E1).onTransition {
144144
semaphore.tryAcquire(4)
145145
}
146146

@@ -168,7 +168,7 @@ class StateMachineTest {
168168
.connect(State.S1, State.S2, Event.E1)
169169
.build()
170170

171-
fsm.onEvent(State.S1, Event.E1, ::toggle)
171+
fsm.onEvent(State.S1, Event.E1).onTransition(::toggle)
172172
assertTrue(executed)
173173
}
174174

@@ -184,7 +184,7 @@ class StateMachineTest {
184184
.connect(State.S1, State.S2, Event.E1)
185185
.build()
186186

187-
fsm.onEvent(State.S1, Event.E1) { toggle() }
187+
fsm.onEvent(State.S1, Event.E1).onTransition { toggle() }
188188
assertTrue(executed)
189189
}
190190

@@ -277,7 +277,7 @@ class StateMachineTest {
277277
.connect(State.S1, State.S2, Event.E1)
278278
.build()
279279

280-
fsm.onEvent(State.S1, Event.E1) { throw exception }
280+
fsm.onEvent(State.S1, Event.E1).onTransition { throw exception }
281281
}
282282
}
283283

0 commit comments

Comments
 (0)