UNPKG

eventric

Version:

Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.

87 lines (69 loc) 2.83 kB
var InMemoryRemoteClient, InMemoryRemoteEndpoint, PubSub, customRemoteBridge, getFullEventName, pubSub, __slice = [].slice; PubSub = require('eventric/src/pub_sub'); customRemoteBridge = null; pubSub = new PubSub; InMemoryRemoteEndpoint = (function() { function InMemoryRemoteEndpoint() { customRemoteBridge = (function(_this) { return function(rpcRequest) { return new Promise(function(resolve, reject) { return _this._handleRPCRequest(rpcRequest, function(error, result) { if (error) { return reject(error); } return resolve(result); }); }); }; })(this); } InMemoryRemoteEndpoint.prototype.setRPCHandler = function(_handleRPCRequest) { this._handleRPCRequest = _handleRPCRequest; }; InMemoryRemoteEndpoint.prototype.publish = function() { var aggregateId, context, domainEventName, fullEventName, payload, _arg, _i; context = arguments[0], _arg = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), payload = arguments[_i++]; domainEventName = _arg[0], aggregateId = _arg[1]; fullEventName = getFullEventName(context, domainEventName, aggregateId); return pubSub.publish(fullEventName, payload, function() {}); }; return InMemoryRemoteEndpoint; })(); module.exports.endpoint = new InMemoryRemoteEndpoint; InMemoryRemoteClient = (function() { function InMemoryRemoteClient() {} InMemoryRemoteClient.prototype.rpc = function(rpcRequest, callback) { if (!customRemoteBridge) { throw new Error('No Remote Endpoint available for in memory client'); } return customRemoteBridge(rpcRequest).then(function(result) { return callback(null, result); })["catch"](function(error) { return callback(error); }); }; InMemoryRemoteClient.prototype.subscribe = function() { var aggregateId, context, domainEventName, fullEventName, handlerFn, _arg, _i; context = arguments[0], _arg = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), handlerFn = arguments[_i++]; domainEventName = _arg[0], aggregateId = _arg[1]; fullEventName = getFullEventName(context, domainEventName, aggregateId); return pubSub.subscribe(fullEventName, handlerFn); }; InMemoryRemoteClient.prototype.unsubscribe = function(subscriberId) { return pubSub.unsubscribe(subscriberId); }; return InMemoryRemoteClient; })(); module.exports.client = new InMemoryRemoteClient; getFullEventName = function(context, domainEventName, aggregateId) { var fullEventName; fullEventName = context; if (domainEventName) { fullEventName += "/" + domainEventName; } if (aggregateId) { fullEventName += "/" + aggregateId; } return fullEventName; };