-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.d.ts
86 lines (70 loc) · 3.28 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
declare const Finity: {
configure(): StateMachineConfigurator<string, string>;
configure<S, E>(): StateMachineConfigurator<S, E>;
start<S, E>(config: Configuration<S, E>): StateMachine<S, E>;
};
export default Finity;
export interface BaseConfigurator<S, E> {
getConfig(): Configuration<S, E>;
}
export interface StateMachineConfigurator<S, E> extends BaseConfigurator<S, E> {
global(): GlobalConfigurator<S, E>;
initialState(state: S): StateConfigurator<S, E>;
state(state: S): StateConfigurator<S, E>;
start(): StateMachine<S, E>;
}
export type StateHook<S, E> = (state: S, context: Context<S, E>) => void;
export type TransitionHook<S, E> = (fromState: S, toState: S, context: Context<S, E>) => void;
export type UnhandledEventHook<S, E> = (event: E, state: S, context: Context<S, E>) => void;
export interface GlobalConfigurator<S, E> extends BaseConfigurator<S, E>, StateMachineConfigurator<S, E> {
onStateEnter(hook: StateHook<S, E>): GlobalConfigurator<S, E>;
onStateExit(hook: StateHook<S, E>): GlobalConfigurator<S, E>;
onTransition(hook: TransitionHook<S, E>): GlobalConfigurator<S, E>;
onStateChange(hook: TransitionHook<S, E>): GlobalConfigurator<S, E>;
onUnhandledEvent(hook: UnhandledEventHook<S, E>): GlobalConfigurator<S, E>;
}
export type StateAction<S, E> = (state: S, context: Context<S, E>) => void;
export type AsyncOperation<S, E> = (state: S, context: Context<S, E>) => Promise<any>;
export interface StateConfigurator<S, E> extends BaseConfigurator<S, E>, StateMachineConfigurator<S, E> {
onEnter(action: StateAction<S, E>): StateConfigurator<S, E>;
onExit(action: StateAction<S, E>): StateConfigurator<S, E>;
on(event: E): TriggerConfigurator<S, E>;
onAny(): TriggerConfigurator<S, E>;
onTimeout(timeout: number): TimerConfigurator<S, E>;
do(asyncOperation: AsyncOperation<S, E>): AsyncConfigurator<S, E>;
submachine<S2>(submachineConfig: Configuration<S2, E>): StateConfigurator<S, E>;
}
export interface TriggerConfigurator<S, E> extends BaseConfigurator<S, E> {
transitionTo(targetState: S): TransitionConfigurator<S, E>;
selfTransition(): TransitionConfigurator<S, E>;
internalTransition(): TransitionConfigurator<S, E>;
ignore(): TransitionConfigurator<S, E>;
}
export interface TimerConfigurator<S, E> extends TriggerConfigurator<S, E> {
}
export interface AsyncConfigurator<S, E> extends BaseConfigurator<S, E> {
onSuccess(): TriggerConfigurator<S, E>;
onFailure(): TriggerConfigurator<S, E>;
}
export type TransitionAction<S, E> = (fromState: S, toState: S, context: Context<S, E>) => void;
export type Condition<S, E> = (context: Context<S, E>) => boolean;
export interface TransitionConfigurator<S, E>
extends BaseConfigurator<S, E>, StateConfigurator<S, E>, TriggerConfigurator<S, E>, AsyncConfigurator<S, E> {
withAction(action: TransitionAction<S, E>): TransitionConfigurator<S, E>;
withCondition(condition: Condition<S, E>): TransitionConfigurator<S, E>;
}
export interface Configuration<S, E> {
}
export interface StateMachine<S, E> {
getCurrentState(): S;
getStateHierarchy(): S[];
canHandle(event: E, eventPayload?: any): boolean;
handle(event: E, eventPayload?: any): StateMachine<S, E>;
}
export interface Context<S, E> {
stateMachine: StateMachine<S, E>;
event?: E;
eventPayload?: any;
result?: any;
error?: Error;
}