Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created March 31, 2014 22:34
Show Gist options
  • Save ktoso/9903970 to your computer and use it in GitHub Desktop.
Save ktoso/9903970 to your computer and use it in GitHub Desktop.
eventsourced processor example
case class ManyCommand(nums: List[Int])
case class State(count: Int) {
def updated(more: Int) = State(count + more)
}
sealed trait Event
case class AddOneEvent(num: Int) extends Event
class MultiCounter extends EventsourcedProcessor {
var state = State(count = 0)
def updateState(e: Event): Any = {
case event: AddOneEvent => state.updated(event.num)
}
def receiveCommand = {
case ManyCommand(many) =>
for(event <- many map AddOneEvent)
persist(event)(updateState(_))
case event: AddOneEvent =>
persist(event)(updateState(_))
}
/** MUST NOT SIDE-EFFECT! */
def receiveRecover = {
case SnapshotOffer(meta, snapshot: State) => this.state = state
case replayedEvent: Event => updateState(replayedEvent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment