Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Command

Concrete implementation of ICommand interface. Command allows to call a method or function using Command pattern.

Example

let command = new Command("add", null, (correlationId, args, callback) => {
    let param1 = args.getAsFloat("param1");
    let param2 = args.getAsFloat("param2");
    let result = param1 + param2;
    callback(null, result);
});

command.execute(
  "123",
  Parameters.fromTuples(
    "param1", 2,
    "param2", 2
  ),
  (err, result) => {
    if (err) console.error(err);
    else console.log("2 + 2 = " + result);
  }
);

// Console output: 2 + 2 = 4
see

ICommand

see

CommandSet

Hierarchy

  • Command

Implements

Index

Constructors

Methods

Constructors

constructor

  • Creates a new command object and assigns it's parameters.

    Parameters

    • name: string

      the command name.

    • schema: Schema

      the schema to validate command arguments.

    • func: any

      the function to be executed by this command.

    Returns Command

Methods

execute

  • execute(correlationId: string, args: Parameters, callback: function): void
  • Executes the command. Before execution it validates args using the defined schema. The command execution intercepts exceptions raised by the called function and returns them as an error in callback.

    see

    Parameters

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • args: Parameters

      the parameters (arguments) to pass to this command for execution.

    • callback: function

      function to be called when command is complete

        • (err: any, result: any): void
        • Parameters

          • err: any
          • result: any

          Returns void

    Returns void

getName

  • getName(): string
  • Gets the command name.

    Returns string

    the name of this command.

validate

  • Validates the command args before execution using the defined schema.

    see

    Parameters

    see

    ValidationResult

    Parameters

    • args: Parameters

      the parameters (arguments) to validate using this command's schema.

    Returns ValidationResult[]

    an array of ValidationResults or an empty array (if no schema is set).

Generated using TypeDoc