Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created April 23, 2019 15:25
Show Gist options
  • Save j5ik2o/d33dedd00da7f303adf169ba5b1e9888 to your computer and use it in GitHub Desktop.
Save j5ik2o/d33dedd00da7f303adf169ba5b1e9888 to your computer and use it in GitHub Desktop.
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