How to access state during transitions in Akka FSM
Asked Answered
F

1

10

I am using Akka FSM for handling state in my Actor. I want some actions to be performed every time a transition to a certain state occurs, no matter which state the transition was made from. After reading the docs, I felt certain that this could be resolved like this:

onTransition({
  case (_, ToState) => performAction(stateData)
})

...

when(FromState){
  case "changestate" => goto(ToState) using NewStateData
}

However, when the transition occurs, stateData is not yet updated to NewStateData.

What is the recommended way to perform actions on entering a certain state?

Frore answered 29/11, 2011 at 11:15 Comment(0)
R
7

Thanks for bringing this to my attention, that was indeed an oversight, which I fixed immediately (see the ticket). Unfortunately there is not much you can do (apart from merging in the tiny patch yourself) until 1.3 is out, which should be next week; if you are particularly impatient, I would appreciate if you could try out RC2 with the fix which will be released this week.

Ramires answered 29/11, 2011 at 15:45 Comment(4)
it took two days longer, but RC2 is out now.Ramires
I appear to have come across this problem in akka 2.3.12. Can you confirm whether new state set using using is available in the respective case of the partial function passed to onTransition()?Stores
It should be available as nextStateData.Ramires
Looking at the Javadoc at doc.akka.io/japi/akka/2.4/index.html, it looks like nextStateData is static. Maybe there is some scala magic that happens, but that is concerning at the least. Does that method work from Java, especially if you have multiple FSM actors?Cramp

© 2022 - 2024 — McMap. All rights reserved.