Created
April 23, 2019 15:25
-
-
Save j5ik2o/d33dedd00da7f303adf169ba5b1e9888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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