curli-bus
Version:
Simple Command Bus Implementation (CQRS) for NodeJS/Typescript.
29 lines (28 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Middleware_1 = require("./Middleware");
const Handlers_1 = require("./Handlers");
class BaseBusSync {
constructor() {
this.middlewareList = new Middleware_1.MiddlewareCollection();
this.handlersCollection = new Handlers_1.HandlersCollection();
this.handlersMiddleware = new Middleware_1.HandlersMiddleware(this.handlersCollection);
}
addMiddleware(middleware) {
this.middlewareList.add(middleware);
}
registerHandler(command, handler) {
const commandName = (typeof command === 'string') ? command : this.getNameOfCommandClass(command);
this.handlersCollection.add(commandName, handler);
}
process(command, options) {
this.middlewareList.add(this.handlersMiddleware);
const result = this.middlewareList.execute(command, options);
this.middlewareList.removeLast();
return result;
}
getNameOfCommandClass(command) {
return command.name;
}
}
exports.BaseBusSync = BaseBusSync;