eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
68 lines (56 loc) • 2.01 kB
JavaScript
var EventBus, PubSub;
PubSub = require('eventric/src/pub_sub');
EventBus = (function() {
function EventBus() {
this._pubSub = new PubSub();
}
EventBus.prototype.subscribeToDomainEventWithAggregateId = function(eventName, aggregateId, handlerFn, options) {
if (options == null) {
options = {};
}
return this.subscribeToDomainEvent("" + eventName + "/" + aggregateId, handlerFn, options);
};
EventBus.prototype.subscribeToDomainEvent = function(eventName, handlerFn, options) {
if (options == null) {
options = {};
}
if (options.isAsync) {
return this._pubSub.subscribeAsync(eventName, handlerFn);
} else {
return this._pubSub.subscribe(eventName, handlerFn);
}
};
EventBus.prototype.subscribeToAllDomainEvents = function(handlerFn) {
return this._pubSub.subscribe('DomainEvent', handlerFn);
};
EventBus.prototype.publishDomainEvent = function(domainEvent, callback) {
if (callback == null) {
callback = function() {};
}
return this._publish('publish', domainEvent, callback);
};
EventBus.prototype.publishDomainEventAndWait = function(domainEvent, callback) {
if (callback == null) {
callback = function() {};
}
return this._publish('publishAsync', domainEvent, callback);
};
EventBus.prototype._publish = function(publishMethod, domainEvent, callback) {
if (callback == null) {
callback = function() {};
}
return this._pubSub[publishMethod]('DomainEvent', domainEvent, (function(_this) {
return function() {
return _this._pubSub[publishMethod](domainEvent.name, domainEvent, function() {
if (domainEvent.aggregate && domainEvent.aggregate.id) {
return _this._pubSub[publishMethod]("" + domainEvent.name + "/" + domainEvent.aggregate.id, domainEvent, callback);
} else {
return callback();
}
});
};
})(this));
};
return EventBus;
})();
module.exports = EventBus;