-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
310 lines (259 loc) · 6.04 KB
/
schema.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// eslint-disable max-len
export type Key_Base = string
export type Enter_Fn <Args extends unknown[], Target> = (...args: Args) => Target
export type Leave_Fn = () => void
export type State_Dscr <Args extends unknown[], Target> =
{
enter: Enter_Fn<Args, Target>,
leave?: Leave_Fn,
}
export type States_Map_Base = { [ key: Key_Base ]: State_Dscr<any[], any> }
export type States_Map_Empty = {}
export type States_Data <T> = T extends States<infer Map> ? Map : never
export type States_Keys <T> = T extends States<infer Map> ? keyof Map : never
export type States_Values <T extends States<any>> = ReturnType<States_Data<T>[ States_Keys<T> ]['enter']>
export type States <States_Map extends States_Map_Base> =
{
add
<
Key extends Key_Base,
Args extends unknown[],
Target,
>
(key: Key, enter_fn: Enter_Fn<Args, Target>, leave_fn?: Leave_Fn)
:
Key extends keyof States_Map
?
never
:
States<States_Map & { [ key in Key ]: State_Dscr<Args, Target> }>,
add
<
Key extends Key_Base,
>
(key: Key)
:
Key extends keyof States_Map
?
never
:
States<States_Map & { [ key in Key ]: State_Dscr<[], void> }>,
keys ()
: readonly (keyof States_Map)[],
has <Key extends Key_Base> (key: Key)
:
Key extends keyof States_Map ? true : false,
get <Key extends keyof States_Map> (key: Key)
:
States_Map[Key],
}
function Void () {}
export function States (states: States_Map_Base = Object.create(null)): States<States_Map_Empty>
{
const $ =
{
add,
keys,
has,
get,
}
function add (
key: Key_Base,
enter_fn?: (...args: unknown[]) => unknown,
leave_fn?: () => void
)
{
if (has(key))
{
throw new TypeError('state_taken_already')
}
return States({ ...states, [ key ]:
{
enter: (enter_fn ?? Void),
leave: leave_fn,
}})
}
function keys ()
{
return Object.keys(states)
}
function has (key: Key_Base)
{
return (key in states)
}
function get (key: Key_Base)
{
if (! has(key)) throw new TypeError('state_missing')
return states[key]
}
return ($ as any)
}
export type Paths_Item = { [ dst: Key_Base ]: true }
export type Paths_Map_Base = { [ src: Key_Base ]: Paths_Item }
export type Paths_Map_Empty = {}
export type Paths_Pair = [ Key_Base, Key_Base ]
export type Paths_Seq = Paths_Pair[]
export type Paths_Base <P extends Paths<any, any>> = P extends Paths<infer Base, any> ? Base : never
export type Paths_Data <P extends Paths<any, any>> = P extends Paths<any, infer Map> ? Map : never
export type Paths <Base extends States<any>, Path extends Paths_Map_Base> =
{
add
<
Src extends States_Keys<Base>,
Dst extends States_Keys<Base>,
>
(src: Src, dst: Dst)
:
Path[Src][Dst] extends true
?
never
:
Paths<Base, Path & { [ src in Src ]: { [ dst in Dst ]: true } }>,
keys ()
: Paths_Seq[],
has
<
Src extends Key_Base,
Dst extends Key_Base,
>
(src: Src, dst: Dst)
:
Path[Src][Dst] extends true ? true : false,
rebase <Base_New extends States<any>> (base: Base_New)
:
Base extends Base_New
?
Paths<Base_New, Path>
:
never,
}
export function Paths <Base extends States<any>> (base: Base, paths: Paths_Seq = []): Paths<Base, Paths_Map_Empty>
{
const $ =
{
add,
keys,
has,
rebase,
}
function add (src: Key_Base, dst: Key_Base)
{
if (! base.has(src)) throw new TypeError('path_wrong_src')
if (! base.has(dst)) throw new TypeError('path_wrong_dst')
if (has(src, dst)) throw new TypeError('path_taken_already')
return Paths(base, [ ...paths, [ src, dst ] ])
}
function keys ()
{
return paths
}
function has (src: Key_Base, dst: Key_Base)
{
return (-1 !== paths.findIndex(([ s, d ]) => (src === s) && (dst === d)))
}
function rebase (base_new: States<any>)
{
if (! based_on(base_new, base)) throw new TypeError('path_wrong_base')
return Paths(base_new, paths)
}
function based_on (base_new: States<any>, base: States<any>)
{
for (var key of base.keys())
{
try
{
if (base_new.get(key) !== base.get(key)) return false
}
catch (e)
{
return false
}
}
return true
}
return ($ as any)
}
export type Schema_States <Sc extends Schema<any, any>> = Sc extends Schema<infer States, any> ? States : never
export type Schema_Paths <Sc extends Schema<any, any>> = Sc extends Schema<any, infer Paths> ? Paths : never
export type Schema_Enter
<
Sc extends Schema<any, any>,
Key extends States_Keys<Schema_States<Sc>>,
>
=
States_Data<Schema_States<Sc>>[Key]['enter']
export type Schema <S extends States<any>, P extends Paths<S, any>> =
{
states: S,
paths: P,
state
<
Key extends Key_Base,
Args extends unknown[],
Target,
>
(key: Key, enter_fn: Enter_Fn<Args, Target>, leave_fn?: Leave_Fn)
:
Key extends States_Keys<S>
?
never
:
Schema<
States<States_Data<S> & { [ key in Key ]: State_Dscr<Args, Target> }>,
Paths<
States<States_Data<S> & { [ key in Key ]: State_Dscr<Args, Target> }>,
Paths_Data<P>
>
>,
state
<
Key extends Key_Base,
>
(key: Key)
:
Key extends States_Keys<S>
?
never
:
Schema<
States<States_Data<S> & { [ key in Key ]: State_Dscr<[], void> }>,
Paths<
States<States_Data<S> & { [ key in Key ]: State_Dscr<[], void> }>,
Paths_Data<P>
>
>,
path
<
Src extends States_Keys<S>,
Dst extends States_Keys<S>,
>
(src: Src, dst: Dst)
:
Paths_Data<P>[Src][Dst] extends true
?
never
:
Schema<S, Paths<S, Paths_Data<P> & { [ src in Src ]: { [ dst in Dst ]: true } }>>,
}
export function Schema <S extends States<any>, P extends Paths<S, any>> (states: S = States() as any, paths: P = Paths(states) as any)
: Schema<States<States_Map_Empty>, Paths<States<States_Map_Empty>, Paths_Map_Empty>>
{
const $ =
{
states,
paths,
state,
path,
}
function state (key: Key_Base, enter_fn?: Enter_Fn<unknown[], unknown>, leave_fn?: Leave_Fn)
{
const states_new = states.add(key, (enter_fn as any), leave_fn)
const paths_new = paths.rebase(states_new)
return Schema(states_new, paths_new)
}
function path (src: States_Keys<S>, dst: States_Keys<S>)
{
return Schema(states, paths.add(src, dst))
}
return ($ as any)
}