Skip to content

Instantly share code, notes, and snippets.

@mnichols
Created February 16, 2017 18:54
Show Gist options
  • Save mnichols/42d864ba5dad042fe3b99ac65cb5ed93 to your computer and use it in GitHub Desktop.
Save mnichols/42d864ba5dad042fe3b99ac65cb5ed93 to your computer and use it in GitHub Desktop.
var command = {
command: 'createContext',
id: '42',
type: 'boards'
}
function raiseEvent(event) {
//persistEvent(event)
publishEvent(event)
}
function createContextHandler(command) {
raiseEvent({
event: 'contextCreated',
id: '42',
type:'boards'
})
}
bus.subscribe('events', function(event) {
readModel.update(event)
}
delete readModel
function raiseEvent(event) {
persistEvent(event)
bus.publishEvent(event)
}
function Thread(id) {
this.comments = []
this.id = id
raiseEvent({ event: 'threadCreated', threadId: id, id: '222', revision: 1, headers: { correlationId: '444'} })
}
Thread.prototype.applyEvent = (event) {
this.revision = event.revision
if(event.event === 'threadCreated'){
this.id = event.id
}
if(event.event === 'commentAppended') {
this.commentsCount++
}
}
Thread.prototype.appendComment = function(comment) {
if(this.commentsCount < 10) {
raiseEvent({ event: 'commentAppended', commentId: '123', text: comment.text, revision: this.revision++ })
raiseEvent({ event: 'threadCompleted', threadId: this.id, revision: this.revision++ })
}
}
var command = { command: 'createThread', id: '123', correlationId: '444'}
function createThreadHandler(command) {
//var thread= loadThreadsById(command.id)
var thread = new Thread(command.id)
}
function loadThreadById(id) {
var events = pullAllEvents(id)
events.forEach(e => { applyEvent(e)})
}
var command1 = { command: 'appendComment',threadId: '123', id: '333', text: 'howdy', correlationId: '222'}
function appendCommentCommandHandler(command) {
//var events = loadEventsForId(command.threadId)
var thread = Thread.loadThreadById(command.threadId).orderBy(revision)
thread.appendComment({ text: command.text })
}
// readmodel
var context = { contextId: <string>, type: <string> , commentIds: []}
var comments = { commentId: <string>, text: <string> }
// readmodel
var context = {
contextId,
type,
comments: [
{ text: <string>, commentId:<string>}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment