forked from kenkunz/svelte-fsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
67 lines (47 loc) · 1.94 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
type BaseState = string | symbol;
type BaseAction = string;
type BaseStates<State extends BaseState = BaseState> = Record<State, BaseActions>;
type Args = any[];
type LifecycleAction = (arg: {
from: BaseState | null;
to: BaseState;
event: BaseAction | null;
args: Args;
}) => void;
type AllArgsAction = (...args: Args) => BaseState;
type VoidFunction = (...args: Args) => void;
type ActionFunction = BaseState | AllArgsAction | VoidFunction;
type BaseActions = {
_enter?: LifecycleAction;
_exit?: LifecycleAction;
[key: BaseAction]: ActionFunction;
};
type DetectFallBackState<State extends BaseState> = State extends '*' ? string : State;
type ExtractStates<States extends BaseStates> = DetectFallBackState<Exclude<keyof States, number>>;
type ExtractObjectValues<Object> = Object[keyof Object];
type GetActionFunctionMapping<Actions extends BaseActions> = {
[Key in Exclude<keyof Actions, '_enter' | '_exit'>]: Actions[Key] extends BaseState
? () => Actions[Key] extends void ? BaseState : Actions[Key]
: Actions[Key] extends VoidFunction
? (...args: Parameters<Actions[Key]>) => BaseState
: Actions[Key];
};
type GetActionMapping<States extends BaseStates> = ExtractObjectValues<{
[Key in keyof States]: GetActionFunctionMapping<States[Key]>;
}>;
type ExtractActions<States extends BaseStates> = GetActionMapping<States>;
type Unsubscribe = () => void;
type Subscribe<S extends BaseState> = (callback: (state: S) => void) => Unsubscribe;
type StateMachine<State extends BaseState, Actions> = {
[Key in keyof Actions]: Actions[Key] | AllArgsAction;
} & {
subscribe: Subscribe<State>;
};
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never;
declare const svelteFsm: <Sts extends Readonly<BaseStates>, S extends ExtractStates<Sts>>(
state: S,
states: Sts
) => StateMachine<ExtractStates<Sts>, UnionToIntersection<ExtractActions<Sts>>>;
export default svelteFsm;