Created
March 31, 2014 22:34
-
-
Save ktoso/9903970 to your computer and use it in GitHub Desktop.
eventsourced processor example
This file contains hidden or 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
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