Skip to content

Commit 7c0c9e6

Browse files
committed
Reshaped and simplified Workflow definition
1 parent bd65823 commit 7c0c9e6

File tree

8 files changed

+280
-268
lines changed

8 files changed

+280
-268
lines changed

src/packages/emmett/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from './testing';
1414
export * from './typing';
1515
export * from './utils';
1616
export * from './validation';
17+
export * from './workflows';

src/packages/emmett/src/typing/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export * from './message';
66
export * from './messageHandling';
77

88
export * from './decider';
9-
export * from './workflow';
109

1110
export type Brand<K, T> = K & { readonly __brand: T };
1211
export type Flavour<K, T> = K & { readonly __brand?: T };

src/packages/emmett/src/typing/workflow.ts

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './workflow';
2+
export * from './workflowProcessor';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { EmmettError } from '../errors';
2+
import type { AnyCommand } from '../typing/command';
3+
import type { AnyEvent } from '../typing/event';
4+
5+
/// Inspired by https://blog.bittacklr.be/the-workflow-pattern.html
6+
7+
export type Workflow<
8+
Input extends AnyEvent | AnyCommand,
9+
State,
10+
Output extends AnyEvent | AnyCommand,
11+
> = {
12+
name?: string;
13+
decide: (command: Input, state: State) => WorkflowOutput<Output>;
14+
evolve: (currentState: State, event: WorkflowEvent<Input | Output>) => State;
15+
initialState: () => State;
16+
};
17+
18+
export type WorkflowEvent<Output extends AnyEvent | AnyCommand> = Extract<
19+
Output,
20+
{ kind?: 'Event' }
21+
>;
22+
23+
export type WorkflowCommand<Output extends AnyEvent | AnyCommand> = Extract<
24+
Output,
25+
{ kind?: 'Command' }
26+
>;
27+
28+
export type WorkflowOutput<Output extends AnyEvent | AnyCommand | EmmettError> =
29+
Output | Output[];
30+
31+
export const Workflow = <
32+
Input extends AnyEvent | AnyCommand,
33+
State,
34+
Output extends AnyEvent | AnyCommand,
35+
>(
36+
workflow: Workflow<Input, State, Output>,
37+
): Workflow<Input, State, Output> => {
38+
return workflow;
39+
};

0 commit comments

Comments
 (0)