@@ -7,15 +7,62 @@ import { CommandBus, CommandEventBus } from './buses';
77import { Command , CommandEvent } from './core' ;
88import { CommandFlowScheduler } from './scheduler' ;
99
10+ /**
11+ * Handler for `Command`s that
12+ * consume commands and optionally produce events.
13+ *
14+ * It can be regarded as a RxJS operator that
15+ * transforms a command into a `CommandEvent` or `void`.
16+ * - when a `CommandEvent` is emitted, the event will be published
17+ * - when a `void` is emitted, nothing will happen
18+ *
19+ * @example
20+ * The built-in RxJS operator `map` is the simplest form of a command handler.
21+ * ```typescript
22+ * const handler: CommandHandler<SomeCommand> = map((command) => {
23+ * const event = new SomeCommandEvent(command);
24+ * return event;
25+ * });
26+ * ```
27+ * `map` can also be used to return `void`:
28+ * ```typescript
29+ * const handler: CommandHandler<SomeCommand> = map((command) => {
30+ * console.log(command);
31+ * });
32+ * ```
33+ *
34+ * @example
35+ * A more complex handler can be created using the `pipe` operator:
36+ * ```typescript
37+ * const handler: CommandHandler<SomeCommand> = pipe(
38+ * filter((command) => someCondition(command)),
39+ * map((command) => new SomeCommandEvent(command)),
40+ * );
41+ * ```
42+ */
1043export interface CommandHandler < C extends Command > {
1144 ( stream : Observable < C > ) : Observable < void | CommandEvent < C > > ;
1245}
1346
14- export interface CommandEventHandler < E extends CommandEvent > {
15- ( stream : Observable < E > ) : Observable < void | Command > ;
16- }
17-
18- export function registerCommandHandler < Types extends Type < Command > [ ] > (
47+ /**
48+ * Register a `CommandHandler` for some types of commands.
49+ * @param types array of command types to listen for
50+ * @remarks Requires an injection context.
51+ *
52+ * @example
53+ * ```typescript
54+ * onCommand([SomeCommand, AnotherCommand], map((command) => {
55+ * console.log(command);
56+ * }));
57+ * ```
58+ * @example
59+ * ```typescript
60+ * onCommand([SomeCommand, AnotherCommand], map((command) => {
61+ * return new SomeEvent(command);
62+ * }));
63+ * ```
64+ */
65+ export function onCommand < Types extends Type < Command > [ ] > (
1966 types : Types ,
2067 handler : CommandHandler < InstanceType < Types [ number ] > > ,
2168) : void {
@@ -33,7 +80,67 @@ export function registerCommandHandler<Types extends Type<Command>[]>(
3380 . subscribe ( ) ;
3481}
3582
36- export function registerCommandEventHandler < Types extends Type < CommandEvent > [ ] > (
83+ /**
84+ * @deprecated Use `onCommand` instead.
85+ */
86+ export const registerCommandHandler = onCommand ;
87+
88+ /**
89+ * Handler for `CommandEvent`s that
90+ * consume events and optionally produce new commands.
91+ *
92+ * It can be regarded as a RxJS operator that
93+ * transforms a command event into a `Command` or `void`.
94+ * - when a `Command` is emitted, the command will be dispatched
95+ * - when a `void` is emitted, nothing will happen
96+ *
97+ * @example
98+ * The built-in RxJS operator `map` is the simplest form of a command event handler.
99+ * ```typescript
100+ * const handler: CommandEventHandler<SomeCommandEvent> = map((event) => {
101+ * const command = new SomeCommand(event);
102+ * return command;
103+ * });
104+ * ```
105+ * `map` can also be used to return `void`:
106+ * ```typescript
107+ * const handler: CommandEventHandler<SomeCommandEvent> = map((event) => {
108+ * console.log(event);
109+ * });
110+ * ```
111+ *
112+ * @example
113+ * A more complex handler can be created using the `pipe` operator:
114+ * ```typescript
115+ * const handler: CommandEventHandler<SomeCommandEvent> = pipe(
116+ * filter((event) => someCondition(event)),
117+ * map((event) => new SomeCommand(event)),
118+ * );
119+ * ```
120+ */
121+ export interface CommandEventHandler < E extends CommandEvent > {
122+ ( stream : Observable < E > ) : Observable < void | Command > ;
123+ }
124+
125+ /**
126+ * Register a `CommandEventHandler` for some types of command events.
127+ * @param types array of command event types to listen for
128+ * @remarks Requires an injection context.
129+ *
130+ * @example
131+ * ```typescript
132+ * onEvent([SomeCommandEvent, AnotherCommandEvent], map((event) => {
133+ * console.log(event);
134+ * }));
135+ * ```
136+ * @example
137+ * ```typescript
138+ * onEvent([SomeCommandEvent, AnotherCommandEvent], map((event) => {
139+ * return new SomeCommand(event);
140+ * }));
141+ * ```
142+ */
143+ export function onEvent < Types extends Type < CommandEvent > [ ] > (
37144 types : Types ,
38145 handler : CommandEventHandler < InstanceType < Types [ number ] > > ,
39146) : void {
@@ -50,3 +157,8 @@ export function registerCommandEventHandler<Types extends Type<CommandEvent>[]>(
50157 )
51158 . subscribe ( ) ;
52159}
160+
161+ /**
162+ * @deprecated Use `onEvent` instead.
163+ */
164+ export const registerCommandEventHandler = onEvent ;
0 commit comments