From 5ee0cab4091424b8562c45efd7b08fc583ddf40c Mon Sep 17 00:00:00 2001 From: momania Date: Mon, 3 Jan 2011 11:35:27 +0100 Subject: [PATCH] - make transition handler a function taking old and new state avoiding the default use of the transition class - only create transition class when transition listeners are subscribed --- akka-actor/src/main/scala/akka/actor/FSM.scala | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/akka-actor/src/main/scala/akka/actor/FSM.scala b/akka-actor/src/main/scala/akka/actor/FSM.scala index 81078f59be..00bdf75d6e 100755 --- a/akka-actor/src/main/scala/akka/actor/FSM.scala +++ b/akka-actor/src/main/scala/akka/actor/FSM.scala @@ -6,7 +6,7 @@ package akka.actor import akka.util._ import scala.collection.mutable -import java.util.concurrent.{ScheduledFuture, TimeUnit} +import java.util.concurrent.ScheduledFuture object FSM { @@ -124,6 +124,7 @@ trait FSM[S, D] { type StateFunction = scala.PartialFunction[Event[D], State] type Timeout = Option[Duration] + type TransitionHandler = (S, S) => Unit /* DSL */ @@ -245,7 +246,7 @@ trait FSM[S, D] { * Set handler which is called upon each state transition, i.e. not when * staying in the same state. */ - protected final def onTransition(transitionHandler: PartialFunction[Transition[S], Unit]) = { + protected final def onTransition(transitionHandler: TransitionHandler) = { transitionEvent = transitionHandler } @@ -306,8 +307,8 @@ trait FSM[S, D] { case StopEvent(reason, _, _) => log.slf4j.info("Stopping because of reason: {}", reason) } - private var transitionEvent: PartialFunction[Transition[S], Unit] = { - case Transition(from, to) => log.slf4j.debug("Transitioning from state {} to {}", from, to) + private var transitionEvent: TransitionHandler = (from, to) => { + log.slf4j.debug("Transitioning from state {} to {}", from, to) } override final protected def receive: Receive = { @@ -358,9 +359,11 @@ trait FSM[S, D] { terminate(Failure("Next state %s does not exist".format(nextState.stateName))) } else { if (currentState.stateName != nextState.stateName) { - val transition = Transition(currentState.stateName, nextState.stateName) - transitionEvent.apply(transition) - transitionCallBackList.foreach(_ ! transition) + transitionEvent.apply(currentState.stateName, nextState.stateName) + if (!transitionCallBackList.isEmpty) { + val transition = Transition(currentState.stateName, nextState.stateName) + transitionCallBackList.foreach(_ ! transition) + } } applyState(nextState) }