remove andThen overload, #25133

This commit is contained in:
Patrik Nordwall 2018-05-23 16:35:23 +02:00
parent 51c9f8ee42
commit a30e039b04
4 changed files with 48 additions and 26 deletions

View file

@ -58,7 +58,7 @@ object Effect {
* Not for user extension. * Not for user extension.
*/ */
@DoNotInherit @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 */ /* All events that will be persisted in this effect */
def events: im.Seq[Event] 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] = final def andThen(callback: State Unit): Effect[Event, State] =
CompositeEffect(this, SideEffect[Event, State](callback)) 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 */ /** The side effect is to stop the actor */
def andThenStop: Effect[Event, State] = def andThenStop: Effect[Event, State] =
CompositeEffect(this, Effect.stop[Event, State]) CompositeEffect(this, Effect.stop[Event, State])

View file

@ -64,7 +64,7 @@ object PerformanceSpec {
persistenceId = name, persistenceId = name,
"", "",
commandHandler = CommandHandler.command { 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 FailAt(sequence) Effect.none.andThen(_ parameters.failAt = sequence)
case command other(command, parameters) case command other(command, parameters)
}, },
@ -80,7 +80,7 @@ object PerformanceSpec {
def eventSourcedTestPersistenceBehavior(name: String, probe: TestProbe[Command]) = def eventSourcedTestPersistenceBehavior(name: String, probe: TestProbe[Command]) =
behavior(name, probe) { behavior(name, probe) {
case (CommandWithEvent(evt), parameters) case (CommandWithEvent(evt), parameters)
Effect.persist(evt).andThen({ Effect.persist(evt).andThen(_ {
parameters.persistCalls += 1 parameters.persistCalls += 1
if (parameters.every(1000)) print(".") if (parameters.every(1000)) print(".")
if (parameters.shouldFail) throw TE("boom") if (parameters.shouldFail) throw TE("boom")

View file

@ -6,6 +6,7 @@ package akka.persistence.typed.scaladsl
import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext
import scala.concurrent.duration._ import scala.concurrent.duration._
import akka.actor.typed.{ ActorRef, Behavior } import akka.actor.typed.{ ActorRef, Behavior }
import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.ActorContext
@ -71,7 +72,7 @@ object PersistentActorCompileOnlyTest {
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case Cmd(data, sender) case Cmd(data, sender)
Effect.persist(Evt(data)) Effect.persist(Evt(data))
.andThen { .andThen { _
sender ! Ack sender ! Ack
} }
}, },
@ -114,7 +115,7 @@ object PersistentActorCompileOnlyTest {
commandHandler = (ctx: ActorContext[Command], state, cmd) cmd match { commandHandler = (ctx: ActorContext[Command], state, cmd) cmd match {
case DoSideEffect(data) case DoSideEffect(data)
Effect.persist(IntentRecorded(state.nextCorrelationId, data)).andThen { Effect.persist(IntentRecorded(state.nextCorrelationId, data)).andThen { _
performSideEffect(ctx.self, state.nextCorrelationId, data) performSideEffect(ctx.self, state.nextCorrelationId, data)
} }
case AcknowledgeSideEffect(correlationId) case AcknowledgeSideEffect(correlationId)
@ -223,7 +224,7 @@ object PersistentActorCompileOnlyTest {
commandHandler = (ctx, _, cmd) cmd match { commandHandler = (ctx, _, cmd) cmd match {
case RegisterTask(task) case RegisterTask(task)
Effect.persist(TaskRegistered(task)) Effect.persist(TaskRegistered(task))
.andThen { .andThen { _
val child = ctx.spawn[Nothing](worker(task), task) val child = ctx.spawn[Nothing](worker(task), task)
// This assumes *any* termination of the child may trigger a `TaskDone`: // This assumes *any* termination of the child may trigger a `TaskDone`:
ctx.watchWith(child, TaskDone(task)) ctx.watchWith(child, TaskDone(task))
@ -278,7 +279,7 @@ object PersistentActorCompileOnlyTest {
def addItem(id: Id, self: ActorRef[Command]) = def addItem(id: Id, self: ActorRef[Command]) =
Effect Effect
.persist[Event, List[Id]](ItemAdded(id)) .persist[Event, List[Id]](ItemAdded(id))
.andThen(metadataRegistry ! GetMetaData(id, adapt)) .andThen(_ metadataRegistry ! GetMetaData(id, adapt))
PersistentBehaviors.receive[Command, Event, List[Id]]( PersistentBehaviors.receive[Command, Event, List[Id]](
persistenceId = "basket-1", persistenceId = "basket-1",
@ -348,7 +349,7 @@ object PersistentActorCompileOnlyTest {
Effect.none Effect.none
case CheerUp(sender) case CheerUp(sender)
changeMoodIfNeeded(state, Happy) changeMoodIfNeeded(state, Happy)
.andThen { .andThen { _
sender ! Ack sender ! Ack
} }
case Remember(memory) case Remember(memory)
@ -382,19 +383,44 @@ object PersistentActorCompileOnlyTest {
class State 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]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "myPersistenceId", persistenceId = "myPersistenceId",
emptyState = new State, emptyState = new State,
commandHandler = CommandHandler.command { commandHandler,
case Enough eventHandler)
Effect.persist(Done) }
.andThen(println("yay"))
.andThenStop
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 = { eventHandler = {
case (state, Done) state case (_: First, _) new Second
case (state, _) state
}) })
} }
} }

View file

@ -116,14 +116,14 @@ object PersistentBehaviorSpec {
case IncrementThenLogThenStop case IncrementThenLogThenStop
Effect.persist(Incremented(1)) Effect.persist(Incremented(1))
.andThen { .andThen { (_: State)
loggingActor ! firstLogging loggingActor ! firstLogging
} }
.andThenStop .andThenStop
case IncrementTwiceThenLogThenStop case IncrementTwiceThenLogThenStop
Effect.persist(Incremented(1), Incremented(2)) Effect.persist(Incremented(1), Incremented(2))
.andThen { .andThen { (_: State)
loggingActor ! firstLogging loggingActor ! firstLogging
} }
.andThenStop .andThenStop
@ -160,30 +160,30 @@ object PersistentBehaviorSpec {
case IncrementTwiceAndThenLog case IncrementTwiceAndThenLog
Effect Effect
.persist(Incremented(1), Incremented(1)) .persist(Incremented(1), Incremented(1))
.andThen { .andThen { (_: State)
loggingActor ! firstLogging loggingActor ! firstLogging
} }
.andThen { .andThen { _
loggingActor ! secondLogging loggingActor ! secondLogging
} }
case EmptyEventsListAndThenLog case EmptyEventsListAndThenLog
Effect Effect
.persist(List.empty) // send empty list of events .persist(List.empty) // send empty list of events
.andThen { .andThen { _
loggingActor ! firstLogging loggingActor ! firstLogging
} }
case DoNothingAndThenLog case DoNothingAndThenLog
Effect Effect
.none .none
.andThen { .andThen { _
loggingActor ! firstLogging loggingActor ! firstLogging
} }
case LogThenStop case LogThenStop
Effect.none Effect.none[Event, State]
.andThen { .andThen { _
loggingActor ! firstLogging loggingActor ! firstLogging
} }
.andThenStop .andThenStop