vulcain-corejs
Version:
Vulcain micro-service framework
71 lines • 3.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const annotations_1 = require("./../di/annotations");
const RX = require("rxjs");
const system_1 = require("../globals/system");
const conventions_1 = require("../utils/conventions");
var EventNotificationMode;
(function (EventNotificationMode) {
EventNotificationMode[EventNotificationMode["successOnly"] = 1] = "successOnly";
EventNotificationMode[EventNotificationMode["never"] = 2] = "never";
EventNotificationMode[EventNotificationMode["always"] = 3] = "always";
})(EventNotificationMode = exports.EventNotificationMode || (exports.EventNotificationMode = {}));
class MessageBus {
constructor(manager, hasAsyncActions) {
this.manager = manager;
this._eventQueuesByDomain = new Map();
this.commandBus = manager.container.get(annotations_1.DefaultServiceNames.ActionBusAdapter);
if (this.commandBus && hasAsyncActions) {
this.commandBus.consumeTask(manager.domain.name, system_1.Service.fullServiceName, manager.processAsyncTask.bind(manager));
}
this.eventBus = manager.container.get(annotations_1.DefaultServiceNames.EventBusAdapter);
}
static get localEvents() {
if (!MessageBus._localEvents) {
MessageBus._localEvents = new RX.Subject();
}
return MessageBus._localEvents;
}
static emitLocalEvent(eventHandlerName, evt) {
if (!evt || evt[MessageBus.LocalEventSymbol])
return;
evt[MessageBus.LocalEventSymbol] = eventHandlerName;
if (!MessageBus._localEvents)
return;
MessageBus._localEvents.next(evt);
}
/**
* Get event queue for a domain
*/
getOrCreateEventQueue(domain, distributionKey) {
let events = this._eventQueuesByDomain.get(domain);
if (!events) {
events = new RX.Subject();
this._eventQueuesByDomain.set(domain, events);
this.eventBus.consumeEvents(domain, this.consumeEvent.bind(this), distributionKey);
}
return events;
}
consumeEvent(event, distributionKey) {
try {
this.getOrCreateEventQueue(event.domain, distributionKey).next(event);
}
catch (e) {
system_1.Service.log.error(null, e, () => `Consume event action: ${event.action} ${event.schema ? "schema: " + event.schema : ""} tenant: ${event.userContext.tenant}`);
}
}
pushTask(command) {
command.status = "Pending";
command.taskId = conventions_1.Conventions.getRandomId();
this.commandBus && this.commandBus.publishTask(command.domain, system_1.Service.fullServiceName, command);
}
sendEvent(event) {
event.inputSchema = null;
event.eventId = conventions_1.Conventions.getRandomId();
this.eventBus.sendEvent(event.domain, event);
return event;
}
}
MessageBus.LocalEventSymbol = Symbol("local_event");
exports.MessageBus = MessageBus;
//# sourceMappingURL=messageBus.js.map