forked from devanshj/txstate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scxml.ts
75 lines (61 loc) · 1.57 KB
/
scxml.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
type TODO = any;
declare const machine:
<C extends Config<C>>(config: C) => C & MachineTag;
declare const childMachine:
<C extends Config<C>>(config: C) => C & ChildMachineTag;
interface MachineTag
{ __type: "Machine" }
interface ChildMachineTag
{ __type: "ChildMachine" }
interface Config<Config>
{ readonly initial: InitialState<Config>
, readonly states: States<Config>
};
type InitialState<Config> =
keyof Prop<Config, "states">
interface States<Config>
{ readonly [S: string]: State<Config> };
type State<Config> =
| StateWithoutChildMachine<Config>
| StateWithChildMachine<Config>
interface StateWithChildMachine<Config>
extends StateBase<Config>, ChildMachineTag {};
interface StateWithoutChildMachine<Config>
extends StateBase<Config>, UseChildMachineFactoryToFixThisError {};
interface StateBase<Config>
{ readonly on?:
{ readonly [E: string]: keyof Prop<Config, "states">
}
}
interface UseChildMachineFactoryToFixThisError
{ initial?: never
, states?: never
}
type Prop<T, K> = K extends keyof T ? T[K] : never;
const lightMachine = machine({
initial: "green",
states: {
green: { on: { TIMER: "yellow" } },
yellow: { on: { TIMER: "red" } },
red: {
on: { TIMER: "green" },
...childMachine({
initial: "stop",
states: {
walk: {
on: {
PED_COUNTDOWN: "wait"
}
},
wait: {
on: {
PED_COUNTDOWN: "stop"
}
},
stop: {},
blinking: {}
}
})
}
}
})