Skip to content

Commit 78f3db8

Browse files
committed
Add runDelayingException() helper function
1 parent 1ebe5b9 commit 78f3db8

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

kstatemachine/src/main/kotlin/ru/nsk/kstatemachine/InternalState.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ internal fun InternalState.isNeighbor(state: IState) = parent?.states?.contains(
3737
internal fun InternalState.requireParent() = requireNotNull(internalParent) { "$this parent is not set" }
3838

3939
internal fun InternalState.stateNotify(block: IState.Listener.() -> Unit) {
40-
listeners.forEach {
41-
try {
42-
it.block()
43-
} catch (e: Exception) {
44-
val machine = machine as InternalStateMachine
45-
machine.delayListenerException(e)
46-
}
47-
}
40+
val machine = machine as InternalStateMachine
41+
listeners.forEach { machine.runDelayingException { it.block() } }
4842
}
4943

5044
internal fun <E : Event> InternalState.findTransitionsByEvent(event: E): List<InternalTransition<E>> {

kstatemachine/src/main/kotlin/ru/nsk/kstatemachine/InternalTransition.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ interface InternalTransition<E : Event> : Transition<E> {
88
fun produceTargetStateDirection(policy: TransitionDirectionProducerPolicy<E>): TransitionDirection
99
}
1010

11-
internal fun InternalTransition<*>.transitionNotify(block: Transition.Listener.() -> Unit) =
12-
listeners.forEach {
13-
try {
14-
it.block()
15-
} catch (e: Exception) {
16-
val machine = sourceState.machine as InternalStateMachine
17-
machine.delayListenerException(e)
18-
}
19-
}
11+
internal fun InternalTransition<*>.transitionNotify(block: Transition.Listener.() -> Unit) {
12+
val machine = sourceState.machine as InternalStateMachine
13+
listeners.forEach { machine.runDelayingException { it.block() } }
14+
}

kstatemachine/src/main/kotlin/ru/nsk/kstatemachine/StateMachineImpl.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,12 @@ internal class StateMachineImpl(name: String?, childMode: ChildMode, override va
192192
}
193193

194194
internal fun InternalStateMachine.machineNotify(block: StateMachine.Listener.() -> Unit) {
195-
machineListeners.forEach {
196-
try {
197-
it.block()
198-
} catch (e: Exception) {
199-
delayListenerException(e)
200-
}
201-
}
202-
}
195+
machineListeners.forEach { runDelayingException { it.block() } }
196+
}
197+
198+
internal fun InternalStateMachine.runDelayingException(block: () -> Unit) =
199+
try {
200+
block()
201+
} catch (e: Exception) {
202+
delayListenerException(e)
203+
}

kstatemachine/src/test/kotlin/ru/nsk/kstatemachine/ChoiceStateTest.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.nsk.kstatemachine
22

3+
import io.kotest.assertions.throwables.shouldThrow
34
import io.kotest.core.spec.style.StringSpec
45
import io.mockk.verifySequence
56

@@ -45,17 +46,17 @@ class ChoiceStateTest : StringSpec({
4546
verifySequence { callbacks.onEntryState(State2) }
4647
}
4748

48-
"initial choice state" {
49+
"initial choice state currently not supported" {
4950
val callbacks = mockkCallbacks()
5051

51-
createStateMachine {
52-
val choice = choiceState("choice") { State2 }
53-
setInitialState(choice)
52+
shouldThrow<IllegalStateException> {
53+
createStateMachine {
54+
val choice = choiceState("choice") { State2 }
55+
setInitialState(choice)
5456

55-
addState(State2) { callbacks.listen(this) }
57+
addState(State2) { callbacks.listen(this) }
58+
}
5659
}
57-
58-
verifySequence { callbacks.onEntryState(State2) }
5960
}
6061
}) {
6162
private object State1 : DefaultState()

0 commit comments

Comments
 (0)