Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
object PersistentWalletAggregate {
def behaviorProxy(
id: WalletId,
chargesLimit: Int = Int.MaxValue
): Behavior[CommandRequest] =
Behaviors
.supervise(Behaviors.setup[CommandRequest] { ctx =>
val childRef: ActorRef[CommandRequest] =
ctx.spawn(WalletAggregate.behavior(id, chargesLimit), WalletAggregate.name(id))
ctx.watch(childRef)
EventSourcedBehavior[CommandRequest, Event, Null](
persistenceId = PersistenceId("p-" + id.value.toString),
emptyState = null,
commandHandler = {
case (state, commandRequest: CommandRequest with ToEvent) =>
childRef ! commandRequest
Effect.persist(commandRequest.toEvent)
case (state, commandRequest: CommandRequest) =>
childRef ! commandRequest
Effect.none
},
eventHandler = { (state, event) =>
childRef ! event.toCommandRequest // コマンド処理後すぐにこのハンドラが呼ばれるためメッセージを二重で受け取ってしまう
state
}
).receiveSignal {
case (_, Terminated(c)) if c == childRef =>
Behaviors.stopped
}
}).onFailure[Throwable](SupervisorStrategy.stop)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.