Search code examples
scalaakkafsm

When is onTransition codes executed, exactly? -- Akka, FSM


I have a doubt whether the code in the "onTransition" block (akka FSM) is executed after the new state is reached? or before the new state is reached.

The articles and book I've read mentions the word "during"... which (to me) suggests "before the new state is reached".

Does it really matter?

I guess so... I mean, changing to a new state implies (in most cases) changing the state data. Usually we would change that state data in the event handler (in the "when-case-event" block).

But what if the new state depends on the outcome of calculations / actions performed in the "onTransition" block? In that case, we will have to move that block into the "when-case-event" block.

So, it's not clear for me now...: is there any rule / guidance: what actions should go into the "when-case-event", and what actions should go into the "onTransition"?

addition: I hope in the next revision of Akka Doc, some kind of clarification / guidance on this topic would be included.

Thanks in advance, Raka


Solution

  • In onTransition the old state data is available via stateData, and the new state data is available as nextStateData. The new state cannot be changed by onTransition, but you can send a message to self.