Skip to content

Instantly share code, notes, and snippets.

@Miczeq22
Created July 1, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miczeq22/33979134cdd211254697c93264058f22 to your computer and use it in GitHub Desktop.
Save Miczeq22/33979134cdd211254697c93264058f22 to your computer and use it in GitHub Desktop.
class CommandBus {
constructor(private readonly handlers: CommandHandler<any>[]) {}
public async execute(command: Command<unknown>) {
const handler = this.handlers.find(
(existingHandler) => existingHandler.type === command.type
);
if (!handler) {
throw new Error(`Handler for command: ${command.type} does not exist.`);
}
return handler.execute(command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment