Skip to content

Commit 0cc8e35

Browse files
committed
feat(command-flow): introduce Flow API to replace Effects
1 parent a3559f1 commit 0cc8e35

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

docs/src/content/packages/command-flow/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ Facade:
4242
- `useCommand`
4343
{{ JSDoc.description("packages/command-flow/src/index.ts#useCommand") }}
4444

45+
Flow:
46+
47+
- `Flow`
48+
{{ JSDoc.description("packages/command-flow/src/flow.ts#Flow") }}
49+
- `FlowEffect`
50+
{{ JSDoc.description("packages/command-flow/src/flow.ts#FlowEffect") }}
51+
- `createFlow`
52+
{{ JSDoc.description("packages/command-flow/src/flow.ts#createFlow") }}
53+
- `provideFlows`
54+
{{ JSDoc.description("packages/command-flow/src/flow.ts#provideFlows") }}
55+
4556
### Exported from `@angularity/command-flow/process-flow`
4657

4758
Foundation:

packages/command-flow/effects/src/effects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55

66
/**
77
* A function that registers side-effects (of commands/events).
8+
* @deprecated Use `createFlow` instead.
89
*/
910
export interface Effects {
1011
(): void;
1112
}
1213

1314
/**
1415
* Registers the provided effects in an environment initializer.
16+
* @deprecated Use `provideFlows` instead.
1517
*/
1618
export function provideEffects(
1719
...effectsInput: (Effects | Effects[])[]

packages/command-flow/src/flow.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
EnvironmentProviders,
3+
inject,
4+
Injector,
5+
makeEnvironmentProviders,
6+
provideEnvironmentInitializer,
7+
runInInjectionContext,
8+
} from '@angular/core';
9+
10+
/**
11+
* A collection of effects that are run in an injection context.
12+
*/
13+
export interface Flow {
14+
effects: FlowEffect[];
15+
}
16+
17+
/**
18+
* A function that is run in an injection context and registers some side effects.
19+
*/
20+
export interface FlowEffect {
21+
(): void;
22+
}
23+
24+
/**
25+
* Creates a `Flow` with the given `FlowEffect`s.
26+
* @example
27+
* ```ts
28+
* export const authFlow = createFlow(
29+
* () =>
30+
* onCommand(
31+
* [Alert],
32+
* map((payload) => {
33+
* window.alert(payload.message);
34+
* return AlertCompleted();
35+
* }),
36+
* ),
37+
* (confirm = inject(ConfirmService)) =>
38+
* onCommand(
39+
* [Confirm],
40+
* map((payload) => {
41+
* const result = confirm.launch(payload.message);
42+
* return ConfirmCompleted(result);
43+
* }),
44+
* ),
45+
* );
46+
* ```
47+
*/
48+
export function createFlow(...effects: FlowEffect[]): Flow {
49+
return { effects };
50+
}
51+
52+
/**
53+
* Provides the given flows to an environment.
54+
*/
55+
export function provideFlows(...flows: Flow[]): EnvironmentProviders {
56+
return makeEnvironmentProviders([
57+
provideEnvironmentInitializer(() => {
58+
const injector = inject(Injector);
59+
for (const flow of flows)
60+
for (const effect of flow.effects)
61+
runInInjectionContext(injector, effect);
62+
}),
63+
]);
64+
}

0 commit comments

Comments
 (0)