Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class GameActor(id: GameId) extends PersistentActor {
override val persistenceId = id.value
var game: Game = Game.create(id)
override def receiveCommand = {
case command: GameCommand => handleResult(game.handleCommand(command))
// ...
}
def handleResult(result: Either[GameRulesViolation, Game]) = result match {
case Right(updatedGame) =>
sender() ! CommandAccepted
handleChanges(updatedGame)
case Left(violation) =>
sender() ! CommandRejected(violation)
}
def handleChanges(updatedGame: Game) =
updatedGame.uncommittedEvents.foreach {
persist(_) { ev =>
game = game.applyEvent(ev).markCommitted
publishEvent(ev)
// ...
}
}
def publishEvent(event: GameEvent) = {
system.eventStream.publish(event)
}
override def receiveRecover = {
case ev: GameEvent =>
game = game.applyEvent(ev)
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment