Lime Web Components API Documentation - v6.24.0
    Preparing search index...

    Interface CommandBus

    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:

    • Register commands with their handlers
    • Execute commands through their registered handlers
    • Support middleware for command pipeline
    • Dispatch command lifecycle events
    • Provide command metadata and discovery
    // Register and execute a command
    const commandBus = platform.get(PlatformServiceName.CommandBus);

    if (commandBus.isSupported(SendEmailCommand)) {
    const command = new SendEmailCommand(context, 'Subject', 'Body');
    await commandBus.handle(command);
    }
    // Register a custom command handler
    const commandBus = platform.get(PlatformServiceName.CommandBus);

    const handler = {
    handle: async (command: GenerateReportCommand) => {
    console.log('Generating report...');
    return { success: true, reportUrl: '/reports/123.pdf' };
    }
    };

    commandBus.register(
    GenerateReportCommand,
    handler,
    {
    title: 'Generate Report',
    description: 'Creates a PDF report',
    icon: 'file-pdf'
    }
    );
    interface CommandBus {
        createCommand<Key extends keyof CommandRegistry>(
            config: CommandConfig<CommandRegistry[Key], Key>,
        ): CommandRegistry[Key];
        createCommand<T = any, Key extends string = string>(
            config: CommandConfig<T, Exclude<Key, keyof CommandRegistry>>,
        ): T;
        getAll?(): CommandMetadata[];
        getHandler(commandClass: CommandClass): CommandHandler;
        handle(command: AnyCommand): any;
        isSupported(commandId: CommandIdentifier): boolean;
        register(commandClass: CommandClass, handler: CommandHandler): void;
        register(
            commandClass: CommandClass,
            handler: CommandHandler,
            metadata?: Omit<CommandMetadata, "id">,
        ): void;
    }

    Hierarchy (View Summary)

    Index

    Methods

    • Create a command instance from a CommandConfig

      Type Parameters

      • Key extends keyof CommandRegistry

      Parameters

      Returns CommandRegistry[Key]

      Thrown if the command has not been registered yet

    • Type Parameters

      • T = any
      • Key extends string = string

      Parameters

      Returns T

    • Execute the given command with it's registered command handler

      Parameters

      Returns any

      result from the command handler

    • Check if a command is supported

      Parameters

      • commandId: CommandIdentifier

        identifier of the command. Can be either the class or the string the class was registered with

      Returns boolean

      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.

      Parameters

      Returns void

      commandBus.register(SendEmailCommand, {
      handle: async (command: SendEmailCommand) => {
      // Send email logic
      return { success: true };
      }
      });
    • Beta

      Register a command to be executed by the given handler

      Parameters

      Returns void