Lime Web Components API Documentation - v7.4.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
    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);

    // Another package may own the handler, so check before building the command.
    if (commandBus.isSupported(SendEmailCommand)) {
    const command = new SendEmailCommand();
    command.context = { limetype: 'person', id: 42 };
    command.subject = 'Meeting Invitation';
    command.body = 'Please join us for a meeting...';

    await commandBus.handle(command);
    }
    import {
    Command,
    CommandHandler,
    PlatformServiceName,
    } from '@limetech/lime-web-components';

    @Command({ id: 'generate-report' })
    class GenerateReportCommand {
    public reportType: string;
    }

    const handler: CommandHandler = {
    handle: (command: GenerateReportCommand) => {
    const http = platform.get(PlatformServiceName.Http);

    return http.post('reports', { type: command.reportType });
    },
    };

    const commandBus = platform.get(PlatformServiceName.CommandBus);

    commandBus.register(GenerateReportCommand, handler, {
    title: 'Generate Report',
    description: 'Creates a PDF report',
    icon: 'file-pdf',
    });

    const command = new GenerateReportCommand();
    command.reportType = 'monthly';

    await commandBus.handle(command);
    interface CommandBus {
        createCommand<Key extends keyof CommandRegistry>(
            config: CommandConfig<CommandRegistry[Key], Key>,
        ): CommandRegistry[Key];
        createCommand<T = unknown, Key extends string = string>(
            config: CommandConfig<T, Exclude<Key, keyof CommandRegistry>>,
        ): T;
        getAll(): CommandMetadata[];
        getHandler(commandClass: CommandClass): CommandHandler;
        handle(command: AnyCommand): unknown;
        isSupported(commandId: CommandIdentifier): boolean;
        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 = unknown
      • Key extends string = string

      Parameters

      Returns T

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

      Parameters

      Returns unknown

      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. Optionally attach CommandMetadata (title, description, icon, etc.) so the command can be presented in command pickers and other UI surfaces.

      Parameters

      Returns void

      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',
      });