UNPKG

eventric

Version:

behavior-first application development

52 lines (43 loc) 1.57 kB
var EventBus; EventBus = (function() { function EventBus(_eventric) { this._eventric = _eventric; this._pubSub = new this._eventric.PubSub(); } EventBus.prototype.subscribeToDomainEvent = function(eventName, handlerFn, options) { if (options == null) { options = {}; } return this._pubSub.subscribe(eventName, handlerFn); }; EventBus.prototype.subscribeToDomainEventWithAggregateId = function(eventName, aggregateId, handlerFn, options) { if (options == null) { options = {}; } return this.subscribeToDomainEvent("" + eventName + "/" + aggregateId, handlerFn, options); }; EventBus.prototype.subscribeToAllDomainEvents = function(handlerFn) { return this._pubSub.subscribe('DomainEvent', handlerFn); }; EventBus.prototype.publishDomainEvent = function(domainEvent) { return new Promise((function(_this) { return function(resolve, reject) { return _this._pubSub.publish('DomainEvent', domainEvent).then(function() { return _this._pubSub.publish(domainEvent.name, domainEvent); }).then(function() { if (domainEvent.aggregate && domainEvent.aggregate.id) { return _this._pubSub.publish("" + domainEvent.name + "/" + domainEvent.aggregate.id, domainEvent).then(function() { return resolve(); }); } else { return resolve(); } })["catch"](function(err) { return reject(err); }); }; })(this)); }; return EventBus; })(); module.exports = EventBus;