UNPKG

2.63 kBTypeScriptView Raw
1import { ICommand } from './ICommand';
2import { Schema } from '../validate/Schema';
3import { Parameters } from '../run/Parameters';
4import { ValidationResult } from '../validate/ValidationResult';
5/**
6 * Concrete implementation of [[ICommand ICommand]] interface. Command allows to call a method
7 * or function using Command pattern.
8 *
9 * ### Example ###
10 *
11 * let command = new Command("add", null, (correlationId, args, callback) => {
12 * let param1 = args.getAsFloat("param1");
13 * let param2 = args.getAsFloat("param2");
14 * let result = param1 + param2;
15 * callback(null, result);
16 * });
17 *
18 * command.execute(
19 * "123",
20 * Parameters.fromTuples(
21 * "param1", 2,
22 * "param2", 2
23 * ),
24 * (err, result) => {
25 * if (err) console.error(err);
26 * else console.log("2 + 2 = " + result);
27 * }
28 * );
29 *
30 * // Console output: 2 + 2 = 4
31 *
32 * @see [[ICommand]]
33 * @see [[CommandSet]]
34 */
35export declare class Command implements ICommand {
36 private _name;
37 private readonly _schema;
38 private readonly _function;
39 /**
40 * Creates a new command object and assigns it's parameters.
41 *
42 * @param name the command name.
43 * @param schema the schema to validate command arguments.
44 * @param func the function to be executed by this command.
45 */
46 constructor(name: string, schema: Schema, func: any);
47 /**
48 * Gets the command name.
49 * @returns the name of this command.
50 */
51 getName(): string;
52 /**
53 * Executes the command. Before execution it validates [[Parameters args]] using
54 * the defined schema. The command execution intercepts exceptions raised
55 * by the called function and returns them as an error in callback.
56 *
57 * @param correlationId (optional) transaction id to trace execution through call chain.
58 * @param args the parameters (arguments) to pass to this command for execution.
59 * @param callback function to be called when command is complete
60 *
61 * @see [[Parameters]]
62 */
63 execute(correlationId: string, args: Parameters, callback: (err: any, result: any) => void): void;
64 /**
65 * Validates the command [[Parameters args]] before execution using the defined schema.
66 *
67 * @param args the parameters (arguments) to validate using this command's schema.
68 * @returns an array of ValidationResults or an empty array (if no schema is set).
69 *
70 * @see [[Parameters]]
71 * @see [[ValidationResult]]
72 */
73 validate(args: Parameters): ValidationResult[];
74}