curli-bus
Version:
Simple Command Bus Implementation (CQRS) for NodeJS/Typescript.
37 lines (28 loc) • 1.08 kB
text/typescript
import {CommandInstanceType, HandlerType, HandlerFunctionType} from '../Types';
export class HandlerModel {
public constructor (
private commandName: string,
private handler: HandlerType | HandlerFunctionType
) {
}
public isThisCommandName (commandName: string): boolean {
return (this.commandName === commandName);
}
public callHandler<T> (command: CommandInstanceType, options?: T): any {
const handler = this.handler;
if (this.determineHandlerIsFunction(handler)) {
return (handler as HandlerFunctionType)(command, options);
} else {
return (handler as HandlerType).handleCommand(command, options);
}
}
private determineHandlerIsFunction (
handler: HandlerType | HandlerFunctionType
): handler is HandlerFunctionType {
// if it doesn't have the method handleCommand, it is a function.
if (!(handler as HandlerType).handleCommand) {
return true;
}
return false;
}
}