eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
54 lines (37 loc) • 1.46 kB
text/coffeescript
PubSub = require 'eventric/src/pub_sub'
customRemoteBridge = null
pubSub = new PubSub
class InMemoryRemoteEndpoint
constructor: ->
customRemoteBridge = (rpcRequest) =>
new Promise (resolve, reject) =>
rpcRequest, (error, result) ->
return reject error if error
resolve result
setRPCHandler: () ->
publish: (context, [domainEventName, aggregateId]..., payload) ->
fullEventName = getFullEventName context, domainEventName, aggregateId
pubSub.publish fullEventName, payload, ->
module.exports.endpoint = new InMemoryRemoteEndpoint
class InMemoryRemoteClient
rpc: (rpcRequest, callback) ->
if not customRemoteBridge
throw new Error 'No Remote Endpoint available for in memory client'
customRemoteBridge rpcRequest
.then (result) ->
callback null, result
.catch (error) ->
callback error
subscribe: (context, [domainEventName, aggregateId]..., handlerFn) ->
fullEventName = getFullEventName context, domainEventName, aggregateId
pubSub.subscribe fullEventName, handlerFn
unsubscribe: (subscriberId) ->
pubSub.unsubscribe subscriberId
module.exports.client = new InMemoryRemoteClient
getFullEventName = (context, domainEventName, aggregateId) ->
fullEventName = context
if domainEventName
fullEventName += "/#{domainEventName}"
if aggregateId
fullEventName += "/#{aggregateId}"
fullEventName