Skip to content

Instantly share code, notes, and snippets.

@pjazdzewski1990
Created May 15, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjazdzewski1990/d47d42bc187f22665317 to your computer and use it in GitHub Desktop.
Save pjazdzewski1990/d47d42bc187f22665317 to your computer and use it in GitHub Desktop.
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