UNPKG

6.65 kBTypeScriptView Raw
1/** @module commands */
2import { ICommand } from './ICommand';
3import { IEvent } from './IEvent';
4import { IEventListener } from './IEventListener';
5import { ICommandInterceptor } from './ICommandInterceptor';
6import { ValidationResult } from '../validate/ValidationResult';
7import { Parameters } from '../run/Parameters';
8/**
9 * Contains a set of commands and events supported by a [[ICommandable commandable]] object.
10 * The CommandSet supports command interceptors to extend and the command call chain.
11 *
12 * CommandSets can be used as alternative commandable interface to a business object.
13 * It can be used to auto generate multiple external services for the business object
14 * without writing much code.
15 *
16 * @see [[Command]]
17 * @see [[Event]]
18 * @see [[ICommandable]]
19 *
20 * ### Example ###
21 *
22 * export class MyDataCommandSet extends CommandSet {
23 * private _controller: IMyDataController;
24 *
25 * constructor(controller: IMyDataController) { // Any data controller interface
26 * super();
27 * this._controller = controller;
28 * this.addCommand(this.makeGetMyDataCommand());
29 * }
30 *
31 * private makeGetMyDataCommand(): ICommand {
32 * return new Command(
33 * 'get_mydata',
34 * null,
35 * (correlationId: string, args: Parameters, callback: (err: any, result: any) => void) => {
36 * let param = args.getAsString('param');
37 * this._controller.getMyData(correlationId, param, callback);
38 * }
39 * );
40 * }
41 * }
42 */
43export declare class CommandSet {
44 private readonly _commands;
45 private readonly _events;
46 private readonly _interceptors;
47 private _commandsByName;
48 private _eventsByName;
49 /**
50 * Creates an empty CommandSet object.
51 */
52 constructor();
53 /**
54 * Gets all commands registered in this command set.
55 *
56 * @returns a list of commands.
57 *
58 * @see [[ICommand]]
59 */
60 getCommands(): ICommand[];
61 /**
62 * Gets all events registred in this command set.
63 *
64 * @returns a list of events.
65 *
66 * @see [[IEvent]]
67 */
68 getEvents(): IEvent[];
69 /**
70 * Searches for a command by its name.
71 *
72 * @param commandName the name of the command to search for.
73 * @returns the command, whose name matches the provided name.
74 *
75 * @see [[ICommand]]
76 */
77 findCommand(commandName: string): ICommand;
78 /**
79 * Searches for an event by its name in this command set.
80 *
81 * @param eventName the name of the event to search for.
82 * @returns the event, whose name matches the provided name.
83 *
84 * @see [[IEvent]]
85 */
86 findEvent(eventName: string): IEvent;
87 private buildCommandChain;
88 private rebuildAllCommandChains;
89 /**
90 * Adds a [[ICommand command]] to this command set.
91 *
92 * @param command the command to add.
93 *
94 * @see [[ICommand]]
95 */
96 addCommand(command: ICommand): void;
97 /**
98 * Adds multiple [[ICommand commands]] to this command set.
99 *
100 * @param commands the array of commands to add.
101 *
102 * @see [[ICommand]]
103 */
104 addCommands(commands: ICommand[]): void;
105 /**
106 * Adds an [[IEvent event]] to this command set.
107 *
108 * @param event the event to add.
109 * @see [[IEvent]]
110 */
111 addEvent(event: IEvent): void;
112 /**
113 * Adds multiple [[IEvent events]] to this command set.
114 *
115 * @param events the array of events to add.
116 *
117 * @see [[IEvent]]
118 */
119 addEvents(events: IEvent[]): void;
120 /**
121 * Adds all of the commands and events from specified [[CommandSet command set]]
122 * into this one.
123 *
124 * @param commandSet the CommandSet to add.
125 */
126 addCommandSet(commandSet: CommandSet): void;
127 /**
128 * Adds a [[IEventListener listener]] to receive notifications on fired events.
129 *
130 * @param listener the listener to add.
131 *
132 * @see [[IEventListener]]
133 */
134 addListener(listener: IEventListener): void;
135 /**
136 * Removes previosly added [[IEventListener listener]].
137 *
138 * @param listener the listener to remove.
139 *
140 * @see [[IEventListener]]
141 */
142 removeListener(listener: IEventListener): void;
143 /**
144 * Adds a [[ICommandInterceptor command interceptor]] to this command set.
145 *
146 * @param interceptor the interceptor to add.
147 *
148 * @see [[ICommandInterceptor]]
149 */
150 addInterceptor(interceptor: ICommandInterceptor): void;
151 /**
152 * Executes a [[ICommand command]] specificed by its name.
153 *
154 * @param correlationId (optional) transaction id to trace execution through call chain.
155 * @param commandName the name of that command that is to be executed.
156 * @param args the parameters (arguments) to pass to the command for execution.
157 * @param callback the function that is to be called once execution is complete. If an exception is raised, then
158 * it will be called with the error (for example: a ValidationException can be thrown).
159 *
160 * @see [[ICommand]]
161 * @see [[Parameters]]
162 */
163 execute(correlationId: string, commandName: string, args: Parameters, callback: (err: any, result: any) => void): void;
164 /**
165 * Validates [[Parameters args]] for command specified by its name using defined schema.
166 * If validation schema is not defined than the methods returns no errors.
167 * It returns validation error if the command is not found.
168 *
169 * @param commandName the name of the command for which the 'args' must be validated.
170 * @param args the parameters (arguments) to validate.
171 * @returns an array of ValidationResults. If no command is found by the given
172 * name, then the returned array of ValidationResults will contain a
173 * single entry, whose type will be ValidationResultType.Error.
174 *
175 * @see [[Command]]
176 * @see [[Parameters]]
177 * @see [[ValidationResult]]
178 */
179 validate(commandName: string, args: Parameters): ValidationResult[];
180 /**
181 * Fires event specified by its name and notifies all registered
182 * [[IEventListener listeners]]
183 *
184 * @param correlationId (optional) transaction id to trace execution through call chain.
185 * @param eventName the name of the event that is to be fired.
186 * @param args the event arguments (parameters).
187 */
188 notify(correlationId: string, eventName: string, args: Parameters): void;
189}