Skip to content

Commit b5e7938

Browse files
Федотов Михаил ВикторовичФедотов Михаил Викторович
authored andcommitted
Fix onFinished() execution order
onFinished() callback should be triggered after onStateChanged().
1 parent 6901244 commit b5e7938

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,12 @@ open class BaseStateImpl(override val name: String?, override val childMode: Chi
203203
state.doEnter(transitionParams)
204204

205205
val machine = machine as InternalStateMachine
206-
if (finish) stateNotify { onFinished(transitionParams) }
207-
208206
machine.machineNotify { onStateChanged(state) }
209207

210-
if (finish) internalParent?.afterChildFinished(this, transitionParams)
208+
if (finish) {
209+
stateNotify { onFinished(transitionParams) }
210+
internalParent?.afterChildFinished(this, transitionParams)
211+
}
211212
}
212213

213214
internal fun switchToTargetState(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,32 @@ class StateMachineTest : StringSpec({
337337
callbacks.onEntryState(state1)
338338
}
339339
}
340+
341+
"state machine listener callbacks sequence" {
342+
val callbacks = mockkCallbacks()
343+
lateinit var state1: State
344+
lateinit var state2: State
345+
346+
val machine = createStateMachine {
347+
logger = StateMachine.Logger { println(it) }
348+
349+
state1 = initialState("state1") {
350+
transitionOn<SwitchEvent> { targetState = { state2 } }
351+
}
352+
state2 = finalState("state2")
353+
354+
onStarted { callbacks.onStarted(this) }
355+
onStateChanged { callbacks.onStateChanged(it) }
356+
onFinished { callbacks.onFinished(this) }
357+
}
358+
359+
machine.processEvent(SwitchEvent)
360+
361+
verifySequence {
362+
callbacks.onStarted(machine)
363+
callbacks.onStateChanged(state1)
364+
callbacks.onStateChanged(state2)
365+
callbacks.onFinished(machine)
366+
}
367+
}
340368
})

0 commit comments

Comments
 (0)