CommandSet

Contains a set of commands and events supported by a commandable object. The CommandSet supports command interceptors and command call chains.

Description

The CommandSet class allows you to create a set of commands and events supported by a commandable object. In addition, it supports command interceptors and command call chains.

Important points

  • CommandSets can be used as an alternative commandable interface to a business object.
  • This class can be used to auto generate multiple external services for a business object.

Constructors

Creates an empty CommandSet object.

public constructor()

Instance methods

addCommand

Adds a command to this command set.
See ICommand

public addCommand(command: ICommand): void

  • command: ICommand - the command to add.

addCommandSet

Adds all of the commands and events from specified command set into this one.

public addCommandSet(commandSet: CommandSet): void

addCommands

Adds multiple commands to this command set.
See ICommand

public addCommands(commands: ICommand[]): void

  • commands: ICommand[] - the array of commands to add.

addEvent

Adds an event to this command set.
See IEvent

public addEvent(event: IEvent): void

  • event: IEvent - the event to add.

addEvents

Adds multiple events to this command set.
See IEvent

public addEvents(events: IEvent[]): void

  • event: IEvent[] - the array of events to add.

addInterceptor

Adds a command interceptor to this command set.

public addInterceptor(interceptor: ICommandInterceptor): void

addListener

Adds a listener to receive notifications on fired events.
See IEventListener

public addListener(listener: IEventListener): void

execute

Executes a command specificed by its name.
See ICommand, Parameters

public execute(correlationId: string, commandName: string, args: Parameters): Promise<any>

  • correlationId: string - (optional) transaction id to trace execution through call chain.
  • commandName: string - the name of that command that is to be executed.
  • args: Parameters - the parameters (arguments) to pass to the command for execution.
  • returns: Promise<any> - the execution result

findCommand

Searches for a command by its name.
See ICommand

public findCommand(commandName: string): ICommand

  • commandName: ICommand - the name of the command to search for.
  • returns: ICommand - the command, whose name matches the provided name.

findEvent

Searches for an event by its name in this command set.

public findEvent(eventName: string): IEvent

  • eventName: string - the name of the event to search for.
  • returns: IEvent - the event, whose name matches the provided name.

getCommands

Gets all commands registered in this command set.
See ICommand

public getCommands(): ICommand[]

  • returns: ICommand[] - a list of commands.

getEvents

Gets all events registred in this command set.
See IEvent

public getEvents(): IEvent[]

  • returns: IEvent[] - a list of events.

notify

Fires event specified by its name and notifies all registered listeners

public notify(correlationId: string, eventName: string, args: Parameters): void

  • correlationId: string - (optional) transaction id to trace execution through call chain.
  • eventName: string - the name of the event that is to be fired.
  • args: Parameters - the event arguments (parameters).

removeListener

Removes previosly added listener.
See IEventListener

public removeListener(listener: IEventListener): void

validate

Validates args for command specified by its name using defined schema. If validation schema is not defined than the methods returns no errors. It returns validation error if the command is not found.

public validate(commandName: string, args: Parameters): ValidationResult[]

  • commandName: string - the name of the command for which the ‘args’ must be validated.
  • args: Parameters - the parameters (arguments) to validate.
  • returns: ValidationResult[] - an array of ValidationResults. If no command is found by the given name, then the returned array of ValidationResults will contain a single entry, whose type will be ValidationResultType.Error.

Examples

export class MyDataCommandSet extends CommandSet {
    private _controller: IMyDataController;
 
    constructor(controller: IMyDataController) { // Any data controller interface
        super();
        this._controller = controller;
        this.addCommand(this.makeGetMyDataCommand());
    }   
 
    private makeGetMyDataCommand(): ICommand {
        return new Command(
          'get_mydata',
          null,
          async (correlationId: string, args: Parameters) => Promise<any> {
              let param = args.getAsString('param');
              return await this._controller.getMyData(correlationId, param);
          }
        );
    }
}

See also