CommandBus
The CommandBus handles commands (mutations/actions) in a CQRS architecture.
Class Definition
class CommandBus implements CommandBusInterface {
constructor(props?: CommandBusConstructorProps);
dispatch<TCommand extends UntypedCommandType>(
command: TCommand
): Promise<void>;
register<TCommand extends UntypedCommandType>(
handler: CommandHandlerInterface<TCommand>
): void;
}
Constructor
new CommandBus(props?: CommandBusConstructorProps)
Creates a new CommandBus instance.
Parameters:
props?: CommandBusConstructorProps- Optional configurationinterface CommandBusConstructorProps {
remoteSender?: RemoteCommandSenderInterface | null;
monitoringService?: MonitoringService | null;
}
Example:
const commandBus = new CommandBus();
// or with options
const commandBus = new CommandBus({
remoteSender: myRemoteSender,
monitoringService: myMonitoringService,
});
Methods
register<TCommand>(handler: CommandHandlerInterface<TCommand>): void
Registers a command handler.
Type Parameters:
TCommand- The command type (must extendUntypedCommandType)
Parameters:
handler: CommandHandlerInterface<TCommand>- The handler functioninterface CommandHandlerInterface<TCommand extends UntypedCommandType> {
(command: TCommand): Promise<void>;
}
Example:
commandBus.register(async (command: CreateTodoCommand) => {
// Handle command
await todosRepository.create(command.payload);
});
dispatch<TCommand>(command: TCommand): Promise<void>
Dispatches a command to registered handlers.
Type Parameters:
TCommand- The command type
Parameters:
command: TCommand- The command to dispatch
Returns: Promise<void> - Resolves when the command is handled
Example:
const command = new CreateTodoCommand({ text: "Learn xndr" });
await commandBus.dispatch(command);
Interface
interface CommandBusInterface {
dispatch<TCommand extends UntypedCommandType>(
command: TCommand
): Promise<void>;
register<TCommand extends UntypedCommandType>(
handler: CommandHandlerInterface<TCommand>
): void;
}