Create a command instance from a CommandConfig
The command configuration
Get a list of configs for all registered commands
Get a handler associated with a command
The command class
the handler for the command class
Execute the given command with it's registered command handler
command to execute
result from the command handler
Check if a command is supported
identifier of the command. Can be either the class or the string the class was registered with
true if the command is supported, false otherwise
Register a command to be executed by the given handler.
Associates a command class with a handler that will process instances of that command when CommandBus.handle is called. Optionally attach CommandMetadata (title, description, icon, etc.) so the command can be presented in command pickers and other UI surfaces.
The command class to register
The CommandHandler instance that will execute the command
Optionalmetadata: Omit<CommandMetadata, "id">Optional presentation metadata for the command
import {
Command,
LimeObjectCommand,
LimeObjectContext,
PlatformServiceName,
} from '@limetech/lime-web-components';
@Command({ id: 'send-email' })
class SendEmailCommand implements LimeObjectCommand {
public context: LimeObjectContext;
public subject: string;
public body: string;
}
const commandBus = platform.get(PlatformServiceName.CommandBus);
commandBus.register(SendEmailCommand, {
handle: (command: SendEmailCommand) => {
const http = platform.get(PlatformServiceName.Http);
return http.post('email', {
recipient: command.context.id,
subject: command.subject,
body: command.body,
});
},
});
import {
Command,
CommandHandler,
LimeObjectCommand,
LimeObjectContext,
PlatformServiceName,
} from '@limetech/lime-web-components';
@Command({ id: 'send-email' })
class SendEmailCommand implements LimeObjectCommand {
public context: LimeObjectContext;
public subject: string;
public body: string;
}
const handler: CommandHandler = {
handle: (command: SendEmailCommand) => {
const http = platform.get(PlatformServiceName.Http);
return http.post('email', { recipient: command.context.id });
},
};
const commandBus = platform.get(PlatformServiceName.CommandBus);
// The metadata is what command pickers and other UI surfaces show.
commandBus.register(SendEmailCommand, handler, {
title: 'Send email',
description: 'Send an email to the contact',
icon: 'envelope',
});
Service for registering and executing commands using the command pattern.
The CommandBus is the central hub for the command pattern implementation. It manages command registration, handler lookup, and command execution. Commands are decoupled from their handlers, allowing for flexible middleware chains and cross-cutting concerns like logging, validation, and authorization.
Key responsibilities:
Example: Execute a command only when a handler is registered
Example: Register a handler, then execute the command
See