Skip to content

Commit 08119c7

Browse files
committed
feat(command-flow): shorten handler register function names
1 parent a56486b commit 08119c7

3 files changed

Lines changed: 210 additions & 8 deletions

File tree

packages/command-flow/process-flow/src/handler.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,67 @@ export interface ProcessHandler<P extends Process<any>> {
2424
}
2525

2626
export enum ProcessSchedulingStrategy {
27+
/**
28+
* Processes are handled one after the other.
29+
*/
2730
Sequential,
31+
/**
32+
* Processes are handled all at the same time.
33+
*/
2834
Concurrent,
35+
/**
36+
* Processes are handled one at a time,
37+
* but can be preempted by a new dispatch,
38+
* i.e. the previous handling will be cancelled.
39+
*/
2940
Preemptive,
41+
/**
42+
* Processes are handled one at a time,
43+
* ignoring any new dispatches until the current handling is completed.
44+
*/
3045
Blocking,
3146
}
3247

33-
export function registerProcessHandler<Types extends Type<Process<any>>[]>(
48+
/**
49+
* Register a `ProcessHandler` for some types of processes.
50+
*
51+
* The value returned from the handler will be translated
52+
* into a series of `ProcessEvent`s:
53+
* - `ProcessStarted` when a matching process is dispatched
54+
* - `ProcessCompleted` when the returned promise resolves
55+
* or the returned observable emits a value
56+
* - `ProcessFailed` when the returned promise rejects
57+
* or the returned observable emits an error
58+
*
59+
* @param types array of process types to listen for
60+
* @param scheduling strategy for handling multiple dispatches
61+
* @remarks Requires an injection context.
62+
*
63+
* @example
64+
* ```typescript
65+
* onProcess(
66+
* [SomeProcess, AnotherProcess],
67+
* ProcessSchedulingStrategy.Sequential,
68+
* async (process) => {
69+
* const result = await someLogic(process);
70+
* return result;
71+
* }
72+
* );
73+
* ```
74+
*
75+
* @example
76+
* ```typescript
77+
* onProcess(
78+
* [SomeProcess, AnotherProcess],
79+
* ProcessSchedulingStrategy.Sequential,
80+
* (process) => {
81+
* const observable = someLogic(process);
82+
* return observable;
83+
* }
84+
* );
85+
* ```
86+
F */
87+
export function onProcess<Types extends Type<Process<any>>[]>(
3488
types: Types,
3589
scheduling: ProcessSchedulingStrategy,
3690
handler: ProcessHandler<InstanceType<Types[number]>>,
@@ -68,3 +122,8 @@ export function registerProcessHandler<Types extends Type<Process<any>>[]>(
68122
),
69123
);
70124
}
125+
126+
/**
127+
* @deprecated Use `onProcess` instead.
128+
*/
129+
export const registerProcessHandler = onProcess;

packages/command-flow/query-flow/src/handler.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,37 @@ import {
1414
} from './events';
1515
import { QueryResultOf as ResultOf } from './shared';
1616

17+
/**
18+
* Handler for `Query`s that
19+
* produces an observable of results for the accepted queries.
20+
*/
1721
export interface QueryHandler<Q extends Query<any>> {
1822
(query: Q): Observable<ResultOf<Q>>;
1923
}
2024

21-
export function registerQueryHandler<Types extends Type<Query<any>>[]>(
25+
/**
26+
* Register a `QueryHandler` for some types of queries.
27+
*
28+
* The observable returned from the handler will be translated
29+
* into a series of `QueryEvent`s:
30+
* - `QueryActivated` when a matching query is dispatched
31+
* - `QueryResolved` when the returned observable emits a value
32+
* - `QueryErrored` when the returned observable emits an error
33+
* - `QueryInactivated` when the returned observable completes
34+
*
35+
* The observable returned from the handler will be unsubscribed
36+
* when a `DisposeQuery` command is dispatched for the query.
37+
*
38+
* @remarks Requires an injection context.
39+
*
40+
* @example
41+
* ```typescript
42+
* onQuery([SomeQuery], (query) => {
43+
* return httpClient.get(...);
44+
* });
45+
* ```
46+
*/
47+
export function onQuery<Types extends Type<Query<any>>[]>(
2248
types: Types,
2349
handler: QueryHandler<InstanceType<Types[number]>>,
2450
): void {
@@ -53,3 +79,8 @@ export function registerQueryHandler<Types extends Type<Query<any>>[]>(
5379
),
5480
);
5581
}
82+
83+
/**
84+
* @deprecated Use `onQuery` instead.
85+
*/
86+
export const registerQueryHandler = onQuery;

packages/command-flow/src/handlers.ts

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,62 @@ import { CommandBus, CommandEventBus } from './buses';
77
import { Command, CommandEvent } from './core';
88
import { 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+
*/
1043
export 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

Comments
 (0)