make transform DSL nicer, see #1966

This commit is contained in:
Roland 2012-05-07 18:21:26 +02:00
parent 838eaa4345
commit c3f7aac8f3
4 changed files with 11 additions and 9 deletions

View file

@ -268,7 +268,7 @@ class FSMActorSpec extends AkkaSpec(Map("akka.actor.debug.fsm" -> true)) with Im
startWith(0, 0) startWith(0, 0)
when(0)(transform { when(0)(transform {
case Event("go", _) stay case Event("go", _) stay
} { } using {
case x goto(1) case x goto(1)
}) })
when(1) { when(1) {

View file

@ -253,8 +253,12 @@ trait FSM[S, D] extends Listeners with ActorLogging {
*/ */
protected final def stop(reason: Reason, stateData: D): State = stay using stateData withStopReason (reason) protected final def stop(reason: Reason, stateData: D): State = stay using stateData withStopReason (reason)
protected final def transform(func: StateFunction)(andThen: PartialFunction[State, State]): StateFunction = protected final class TransformHelper(func: StateFunction) {
def using(andThen: PartialFunction[State, State]): StateFunction =
func andThen (andThen orElse { case x x }) func andThen (andThen orElse { case x x })
}
protected final def transform(func: StateFunction): TransformHelper = new TransformHelper(func)
/** /**
* Schedule named timer to deliver message after given delay, possibly repeating. * Schedule named timer to deliver message after given delay, possibly repeating.

View file

@ -128,7 +128,7 @@ class FSMDocSpec extends AkkaSpec {
when(SomeState)(transform { when(SomeState)(transform {
case Event(bytes: Array[Byte], read) stay using (read + bytes.length) case Event(bytes: Array[Byte], read) stay using (read + bytes.length)
case Event(bytes: List[Byte], read) stay using (read + bytes.size) case Event(bytes: List[Byte], read) stay using (read + bytes.size)
} { } using {
case s @ FSM.State(state, read, timeout, stopReason, replies) if read > 1000 case s @ FSM.State(state, read, timeout, stopReason, replies) if read > 1000
goto(Processing) goto(Processing)
}) })
@ -143,7 +143,7 @@ class FSMDocSpec extends AkkaSpec {
when(SomeState)(transform { when(SomeState)(transform {
case Event(bytes: Array[Byte], read) stay using (read + bytes.length) case Event(bytes: Array[Byte], read) stay using (read + bytes.length)
case Event(bytes: List[Byte], read) stay using (read + bytes.size) case Event(bytes: List[Byte], read) stay using (read + bytes.size)
}(processingTrigger)) } using processingTrigger)
//#alt-transform-syntax //#alt-transform-syntax
//#termination-syntax //#termination-syntax

View file

@ -329,8 +329,6 @@ be used several times, e.g. when applying the same transformation to several
.. includecode:: code/akka/docs/actor/FSMDocSpec.scala .. includecode:: code/akka/docs/actor/FSMDocSpec.scala
:include: alt-transform-syntax :include: alt-transform-syntax
The parentheses are required by Scalas syntax rules.
Timers Timers
------ ------