InterceptedCommand

Implements a command wrapped by an interceptor.

Implements: ICommand

Description

The InterceptedCommand allows you to implement a command wrapped by an interceptor. Thus, it allows you to build command call chains, where the interceptor can alter execution and delegate calls to a next command, which can then be intercepted or not.

Constructors

Creates a new InterceptedCommand, which serves as a link in an execution chain. Contains information about the interceptor that is being used and the next command in the chain.

public constructor(interceptor: ICommandInterceptor, next: ICommand)

  • interceptor: ICommandInterceptor - the interceptor that is intercepting the command.
  • next: ICommand - (link to) the next command in the command’s execution chain.

Instance methods

execute

Executes the next command in the execution chain using the given parameters (arguments).
See Parameters

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

  • correlationId: string - unique transaction id to trace calls across components.
  • args: Parameters - the parameters (arguments) to pass to the command for execution.
  • returns: Promise<any> - the execution result

getName

Returns string the name of the command that is being intercepted.

public getName(c): string

  • returns: string - the name of the command that is being intercepted.

validate

Validates the parameters (arguments) that are to be passed to the command that is next in the execution chain.
See Parameters, ValidationResult

public validate(args: Parameters): ValidationResult[]

  • args: Parameters - the parameters (arguments) to validate for the next command.
  • returns: ValidationResult[] - an array of ValidationResults.

Examples

export class CommandLogger implements ICommandInterceptor {       
        
    public getName(command: ICommand): string {
        return command.getName();
    }
          
    public async execute(correlationId: string, command: ICommand, args: Parameters): Promise<any> {
        console.log("Executed command " + command.getName());
        await command.execute(correlationId, args);
    }
          
    private validate(command: ICommand, args: Parameters): ValidationResult[] {
        return command.validate(args);
    }
}
   
let logger = new CommandLogger();
let loggedCommand = new InterceptedCommand(logger, command); 

// Each called command will output: Executed command <command name>

See also