eventric
Version:
behavior-first application development
166 lines (110 loc) • 4.45 kB
text/coffeescript
Context = require './context'
Remote = require './remote'
Projection = require './projection'
uuidGenerator = require './uuid_generator'
remoteContextHash = {}
class Eventric
constructor: ->
= require './logger'
GlobalContext = require './global_context'
inmemoryRemote = require 'eventric-remote-inmemory'
InmemoryStore = require 'eventric-store-inmemory'
= {}
= {}
= {}
= []
= null
= []
= []
= new GlobalContext
inmemoryRemote.endpoint
InmemoryStore, {}
setLogger: (logger) ->
= logger
getLogger: ->
return
setLogLevel: (logLevel) ->
.setLogLevel logLevel
# TODO: Test
setStore: (StoreClass, storeOptions = {}) ->
=
Class: StoreClass
options: storeOptions
# TODO: Test
getStoreDefinition: ->
context: (name) ->
if !name
throw new Error 'Contexts must have a name'
context = new Context name
context.subscribeToAllDomainEvents (domainEvent) =>
domainEvent
[name] = context
context
# TODO: Reconsider/Remove when adding EventStore
initializeGlobalProjections: ->
if not
= new Projection
startOfInitialization = new Date
.debug 'eventric global projections initializing'
initializeGlobalProjectionsPromise = Promise.resolve()
.forEach (globalProjection) =>
initializeGlobalProjectionsPromise = initializeGlobalProjectionsPromise.then =>
.initializeInstance globalProjection, {}
initializeGlobalProjectionsPromise
.then =>
endOfInitialization = new Date
durationOfInitialization = endOfInitialization - startOfInitialization
.debug "eventric global projections initialized after #{durationOfInitialization}ms"
return initializeGlobalProjectionsPromise
# TODO: Reconsider/Remove when adding EventStore
addGlobalProjection: (projectionObject) ->
.push projectionObject
getRegisteredContextNames: ->
Object.keys
setDefaultRemoteClient: (remoteClient) ->
= remoteClient
remoteContext: (contextName) ->
if !contextName
throw new Error 'Missing context name'
return remoteContextHash[contextName] if remoteContextHash[contextName]
remote = remoteContextHash[contextName] = new Remote contextName
if
remote.setClient
return remote
addRemoteEndpoint: (remoteEndpoint) ->
.push remoteEndpoint
remoteEndpoint.setRPCHandler
generateUuid: ->
uuidGenerator.generateUuid()
_handleRemoteRPCRequest: (request, callback) =>
context = [request.contextName]
if not context
error = new Error "Tried to handle Remote RPC with not registered context #{request.contextName}"
.error error, '\n', error.stack
callback error, null
return
if Remote.ALLOWED_RPC_OPERATIONS.indexOf(request.functionName) is -1
error = new Error "RPC operation '#{request.functionName}' not allowed"
.error error, '\n', error.stack
callback error, null
return
if request.functionName not of context
error = new Error "Remote RPC function #{request.functionName} not found on Context #{request.contextName}"
.error error, '\n', error.stack
callback error, null
return
context[request.functionName] request.args...
.then (result) ->
callback null, result
.catch (error) ->
callback error
_delegateDomainEventToRemoteEndpoints: (domainEvent) ->
Promise.all .map (remoteEndpoint) ->
publishPromise = Promise.resolve().then ->
remoteEndpoint.publish domainEvent.context, domainEvent.name, domainEvent
if domainEvent.aggregate
publishPromise = publishPromise.then ->
remoteEndpoint.publish domainEvent.context, domainEvent.name, domainEvent.aggregate.id, domainEvent
return publishPromise
module.exports = new Eventric