diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/Effect.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/Effect.scala index 1d3da4f823..049fc380b9 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/Effect.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/scaladsl/Effect.scala @@ -58,7 +58,7 @@ object Effect { * Not for user extension. */ @DoNotInherit -trait Effect[+Event, State] extends akka.persistence.typed.javadsl.Effect[Event, State] { self: EffectImpl[Event, State] ⇒ +trait Effect[+Event, State] { self: EffectImpl[Event, State] ⇒ /* All events that will be persisted in this effect */ def events: im.Seq[Event] @@ -68,10 +68,6 @@ trait Effect[+Event, State] extends akka.persistence.typed.javadsl.Effect[Event, final def andThen(callback: State ⇒ Unit): Effect[Event, State] = CompositeEffect(this, SideEffect[Event, State](callback)) - /** Convenience method to register a side effect with just a lazy expression */ - final def andThen(callback: ⇒ Unit): Effect[Event, State] = - CompositeEffect(this, SideEffect[Event, State]((_: State) ⇒ callback)) - /** The side effect is to stop the actor */ def andThenStop: Effect[Event, State] = CompositeEffect(this, Effect.stop[Event, State]) diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala index 615dcd9fd0..92fef2c853 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala @@ -64,7 +64,7 @@ object PerformanceSpec { persistenceId = name, "", commandHandler = CommandHandler.command { - case StopMeasure ⇒ Effect.none.andThen(probe.ref ! StopMeasure) + case StopMeasure ⇒ Effect.none.andThen(_ ⇒ probe.ref ! StopMeasure) case FailAt(sequence) ⇒ Effect.none.andThen(_ ⇒ parameters.failAt = sequence) case command ⇒ other(command, parameters) }, @@ -80,7 +80,7 @@ object PerformanceSpec { def eventSourcedTestPersistenceBehavior(name: String, probe: TestProbe[Command]) = behavior(name, probe) { case (CommandWithEvent(evt), parameters) ⇒ - Effect.persist(evt).andThen({ + Effect.persist(evt).andThen(_ ⇒ { parameters.persistCalls += 1 if (parameters.every(1000)) print(".") if (parameters.shouldFail) throw TE("boom") diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala index 9cd7500ea1..54e2e39d81 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala @@ -6,6 +6,7 @@ package akka.persistence.typed.scaladsl import scala.concurrent.ExecutionContext import scala.concurrent.duration._ + import akka.actor.typed.{ ActorRef, Behavior } import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.ActorContext @@ -71,7 +72,7 @@ object PersistentActorCompileOnlyTest { commandHandler = CommandHandler.command { case Cmd(data, sender) ⇒ Effect.persist(Evt(data)) - .andThen { + .andThen { _ ⇒ sender ! Ack } }, @@ -114,7 +115,7 @@ object PersistentActorCompileOnlyTest { commandHandler = (ctx: ActorContext[Command], state, cmd) ⇒ cmd match { case DoSideEffect(data) ⇒ - Effect.persist(IntentRecorded(state.nextCorrelationId, data)).andThen { + Effect.persist(IntentRecorded(state.nextCorrelationId, data)).andThen { _ ⇒ performSideEffect(ctx.self, state.nextCorrelationId, data) } case AcknowledgeSideEffect(correlationId) ⇒ @@ -223,7 +224,7 @@ object PersistentActorCompileOnlyTest { commandHandler = (ctx, _, cmd) ⇒ cmd match { case RegisterTask(task) ⇒ Effect.persist(TaskRegistered(task)) - .andThen { + .andThen { _ ⇒ val child = ctx.spawn[Nothing](worker(task), task) // This assumes *any* termination of the child may trigger a `TaskDone`: ctx.watchWith(child, TaskDone(task)) @@ -278,7 +279,7 @@ object PersistentActorCompileOnlyTest { def addItem(id: Id, self: ActorRef[Command]) = Effect .persist[Event, List[Id]](ItemAdded(id)) - .andThen(metadataRegistry ! GetMetaData(id, adapt)) + .andThen(_ ⇒ metadataRegistry ! GetMetaData(id, adapt)) PersistentBehaviors.receive[Command, Event, List[Id]]( persistenceId = "basket-1", @@ -348,7 +349,7 @@ object PersistentActorCompileOnlyTest { Effect.none case CheerUp(sender) ⇒ changeMoodIfNeeded(state, Happy) - .andThen { + .andThen { _ ⇒ sender ! Ack } case Remember(memory) ⇒ @@ -382,19 +383,44 @@ object PersistentActorCompileOnlyTest { class State + private val commandHandler: CommandHandler[Command, Event, State] = CommandHandler.command { + case Enough ⇒ + Effect.persist(Done) + .andThen((_: State) ⇒ println("yay")) + .andThenStop + } + + private val eventHandler: (State, Event) ⇒ State = { + case (state, Done) ⇒ state + } + PersistentBehaviors.receive[Command, Event, State]( persistenceId = "myPersistenceId", emptyState = new State, - commandHandler = CommandHandler.command { - case Enough ⇒ - Effect.persist(Done) - .andThen(println("yay")) - .andThenStop + commandHandler, + eventHandler) + } + object AndThenPatternMatch { + trait State + class First extends State + class Second extends State + + PersistentBehaviors.receive[String, String, State]( + persistenceId = "myPersistenceId", + emptyState = new First, + commandHandler = CommandHandler.command { + cmd ⇒ + Effect.persist(cmd).andThen { + case _: First ⇒ println("first") + case _: Second ⇒ println("second") + } }, eventHandler = { - case (state, Done) ⇒ state + case (_: First, _) ⇒ new Second + case (state, _) ⇒ state }) + } } diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala index bb50497077..e988632910 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala @@ -116,14 +116,14 @@ object PersistentBehaviorSpec { case IncrementThenLogThenStop ⇒ Effect.persist(Incremented(1)) - .andThen { + .andThen { (_: State) ⇒ loggingActor ! firstLogging } .andThenStop case IncrementTwiceThenLogThenStop ⇒ Effect.persist(Incremented(1), Incremented(2)) - .andThen { + .andThen { (_: State) ⇒ loggingActor ! firstLogging } .andThenStop @@ -160,30 +160,30 @@ object PersistentBehaviorSpec { case IncrementTwiceAndThenLog ⇒ Effect .persist(Incremented(1), Incremented(1)) - .andThen { + .andThen { (_: State) ⇒ loggingActor ! firstLogging } - .andThen { + .andThen { _ ⇒ loggingActor ! secondLogging } case EmptyEventsListAndThenLog ⇒ Effect .persist(List.empty) // send empty list of events - .andThen { + .andThen { _ ⇒ loggingActor ! firstLogging } case DoNothingAndThenLog ⇒ Effect .none - .andThen { + .andThen { _ ⇒ loggingActor ! firstLogging } case LogThenStop ⇒ - Effect.none - .andThen { + Effect.none[Event, State] + .andThen { _ ⇒ loggingActor ! firstLogging } .andThenStop