@@ -3,15 +3,44 @@ import { R } from "./extras"
33
44export type UseStateMachine =
55 < D extends Machine . Definition < D > > ( definition : A . InferNarrowestObject < D > ) =>
6- [ state : A . Instantiated < Machine . State < Machine . Definition . FromTypeParamter < D > > >
7- , send : A . Instantiated < Machine . Send < Machine . Definition . FromTypeParamter < D > > >
8- ]
6+ Machine < Machine . Definition . FromTypeParameter < D > >
97
108export const $$t = Symbol ( "$$t" ) ;
119type $$t = typeof $$t ;
1210export type CreateType = < T > ( ) => { [ $$t ] : T }
1311
12+ export type Machine < D ,
13+ State = Machine . State < D > ,
14+ NextEvents =
15+ ( State extends any
16+ ? A . Get < Machine . ExitEventForState < D , State > , "type" >
17+ : never
18+ ) [ ]
19+ > =
20+ & { nextEvents : NextEvents
21+ , send : Machine . Send < D >
22+ }
23+ & ( State extends any
24+ ? { state : State
25+ , context : Machine . Context < D >
26+ , event : Machine . EntryEventForState < D , State >
27+ , nextEventsT : A . Get < Machine . ExitEventForState < D , State > , "type" > [ ]
28+ }
29+ : never
30+ )
31+
32+ interface MachineImpl
33+ { state : Machine . State . Impl
34+ , context : Machine . Context . Impl
35+ , event : Machine . Event . Impl
36+ , nextEvents : Machine . Event . Impl [ "type" ] [ ]
37+ , nextEventsT : Machine . Event . Impl [ "type" ] [ ]
38+ , send : Machine . Send . Impl
39+ }
40+
1441export namespace Machine {
42+ export type Impl = MachineImpl
43+
1544 export type Definition <
1645 Self ,
1746 States = A . Get < Self , "states" > ,
@@ -52,8 +81,8 @@ export namespace Machine {
5281 )
5382
5483 interface DefinitionImp
55- { initial : StateValue . Impl
56- , states : R . Of < StateValue . Impl , Definition . StateNode . Impl >
84+ { initial : State . Impl
85+ , states : R . Of < State . Impl , Definition . StateNode . Impl >
5786 , on ?: Definition . On . Impl
5887 , schema ?: { context ?: null , events ?: R . Of < Event . Impl [ "type" ] , null > }
5988 , verbose ?: boolean
@@ -64,7 +93,7 @@ export namespace Machine {
6493 export namespace Definition {
6594 export type Impl = DefinitionImp
6695
67- export type FromTypeParamter < D > =
96+ export type FromTypeParameter < D > =
6897 "$$internalIsConstraint" extends keyof D
6998 ? D extends infer X ? X extends Definition < infer X > ? X : never : never
7099 : D
@@ -120,7 +149,7 @@ export namespace Machine {
120149 }
121150
122151 export type Transition < D , P ,
123- TargetString = Machine . StateValue < D > ,
152+ TargetString = Machine . State < D > ,
124153 Event = { type : L . Pop < P > }
125154 > =
126155 | TargetString
@@ -135,12 +164,12 @@ export namespace Machine {
135164 }
136165
137166 type TransitionImpl =
138- | State . Impl [ "value " ]
139- | { target : State . Impl [ "value " ]
167+ | Machine . Impl [ "state " ]
168+ | { target : Machine . Impl [ "state " ]
140169 , guard ?:
141170 ( parameter :
142- { context : State . Impl [ "context" ]
143- , event : State . Impl [ "event" ]
171+ { context : Machine . Impl [ "context" ]
172+ , event : Machine . Impl [ "event" ]
144173 }
145174 ) => boolean
146175 }
@@ -149,10 +178,10 @@ export namespace Machine {
149178 }
150179
151180
152- export type Effect < D , P , StateValue = L . Pop < L . Popped < P > > > =
153- ( parameter : A . Instantiated < EffectParameterForStateValue < D , StateValue > > ) =>
181+ export type Effect < D , P , State = L . Pop < L . Popped < P > > > =
182+ ( parameter : A . Instantiated < EffectParameterForState < D , State > > ) =>
154183 | void
155- | ( ( parameter : A . Instantiated < EffectCleanupParameterForStateValue < D , StateValue > > ) => void )
184+ | ( ( parameter : A . Instantiated < EffectCleanupParameterForState < D , State > > ) => void )
156185
157186 type EffectImpl =
158187 ( parameter : EffectParameter . Impl ) =>
@@ -218,15 +247,15 @@ export namespace Machine {
218247 export type InitialEventType = "$$initial" ;
219248 }
220249
221- export type StateValue < D > =
250+ export type State < D > =
222251 keyof A . Get < D , "states" >
223252
224- export type InitialStateValue < D > =
253+ export type InitialState < D > =
225254 A . Get < D , "initial" >
226255
227- type StateValueImpl = string & A . Tag < "Machine.StateValue " >
228- export namespace StateValue {
229- export type Impl = StateValueImpl ;
256+ type StateImpl = string & A . Tag < "Machine.State " >
257+ export namespace State {
258+ export type Impl = StateImpl ;
230259 }
231260
232261 export type Context < D > =
@@ -240,7 +269,7 @@ export namespace Machine {
240269 export type Event < D , EventsSchema = A . Get < D , [ "schema" , "events" ] , { } > > =
241270 | O . Value < { [ T in U . Exclude < keyof EventsSchema , Definition . ExhaustiveIdentifier > ] :
242271 A . Get < EventsSchema , [ T , $$t ] > extends infer P
243- ? P extends any ? O . ShallowMerge < { type : T } & P > : never
272+ ? P extends any ? O . ShallowClean < { type : T } & P > : never
244273 : never
245274 } >
246275 | ( A . Get < EventsSchema , Definition . ExhaustiveIdentifier , false > extends true ? never :
@@ -271,7 +300,17 @@ export namespace Machine {
271300 }
272301
273302 export namespace EffectParameter {
303+ export interface EffectParameterForState < D , State >
304+ extends BaseEffectParameter < D >
305+ { event : Machine . EntryEventForState < D , State >
306+ }
307+
274308 export namespace Cleanup {
309+ export interface ForState < D , State >
310+ extends BaseEffectParameter < D >
311+ { event : Machine . ExitEventForState < D , State >
312+ }
313+
275314 export type Impl = EffectParameter . Impl
276315 }
277316
@@ -284,14 +323,14 @@ export namespace Machine {
284323 , setContext : SetContext . Impl
285324 }
286325
287- export interface EffectParameterForStateValue < D , StateValue >
326+ export interface EffectParameterForState < D , State >
288327 extends BaseEffectParameter < D >
289- { event : A . Uninstantiated < Machine . EntryEventForStateValue < D , StateValue > >
328+ { event : A . Uninstantiated < Machine . EntryEventForState < D , State > >
290329 }
291330
292- export interface EffectCleanupParameterForStateValue < D , StateValue >
331+ export interface EffectCleanupParameterForState < D , State >
293332 extends BaseEffectParameter < D >
294- { event : A . Uninstantiated < Machine . ExitEventForStateValue < D , StateValue > >
333+ { event : A . Uninstantiated < Machine . ExitEventForState < D , State > >
295334 }
296335
297336 export interface BaseEffectParameter < D >
@@ -300,8 +339,8 @@ export namespace Machine {
300339 , setContext : Machine . SetContext < D >
301340 }
302341
303- export type EntryEventForStateValue < D , StateValue > =
304- | ( StateValue extends InitialStateValue < D >
342+ export type EntryEventForState < D , State > =
343+ | ( State extends InitialState < D >
305344 ? { type : Definition . InitialEventType }
306345 : never
307346 )
@@ -311,27 +350,27 @@ export namespace Machine {
311350 | O . Value < { [ S in keyof A . Get < D , "states" > ] :
312351 O . Value < { [ E in keyof A . Get < D , [ "states" , S , "on" ] > ] :
313352 A . Get < D , [ "states" , S , "on" , E ] > extends infer T
314- ? ( T extends A . String ? T : A . Get < T , "target" > ) extends StateValue
353+ ? ( T extends A . String ? T : A . Get < T , "target" > ) extends State
315354 ? E
316355 : never
317356 : never
318357 } >
319358 } >
320359 | O . Value < { [ E in keyof A . Get < D , [ "on" ] > ] :
321360 A . Get < D , [ "on" , E ] > extends infer T
322- ? ( T extends A . String ? T : A . Get < T , "target" > ) extends StateValue
361+ ? ( T extends A . String ? T : A . Get < T , "target" > ) extends State
323362 ? E
324363 : never
325364 : never
326365 } >
327366 }
328367 >
329368
330- export type ExitEventForStateValue < D , StateValue > =
369+ export type ExitEventForState < D , State > =
331370 U . Extract <
332371 Event < D > ,
333372 { type :
334- | keyof A . Get < D , [ "states" , StateValue , "on" ] , { } >
373+ | keyof A . Get < D , [ "states" , State , "on" ] , { } >
335374 | keyof A . Get < D , "on" , { } >
336375 }
337376 >
@@ -378,34 +417,6 @@ export namespace Machine {
378417 export namespace ContextUpdater {
379418 export type Impl = ContextUpdaterImpl ;
380419 }
381-
382- export type State < D ,
383- Value = StateValue < D > ,
384- NextEvents =
385- ( Value extends any
386- ? A . Get < ExitEventForStateValue < D , Value > , "type" >
387- : never
388- ) [ ]
389- > =
390- Value extends any
391- ? { value : Value
392- , context : A . Uninstantiated < Context < D > >
393- , event : A . Uninstantiated < EntryEventForStateValue < D , Value > >
394- , nextEventsT : A . Get < ExitEventForStateValue < D , Value > , "type" > [ ]
395- , nextEvents : NextEvents
396- }
397- : never
398-
399- interface StateImpl
400- { value : StateValue . Impl
401- , context : Context . Impl
402- , event : Event . Impl
403- , nextEvents : Event . Impl [ "type" ] [ ]
404- , nextEventsT : Event . Impl [ "type" ] [ ]
405- }
406- export namespace State {
407- export type Impl = StateImpl
408- }
409420}
410421
411422export namespace L {
@@ -439,7 +450,8 @@ export namespace U {
439450
440451export namespace O {
441452 export type Value < T > = T [ keyof T ] ;
442- export type ShallowMerge < T > = { [ K in keyof T ] : T [ K ] } & unknown
453+ export type ShallowClean < T > = { [ K in keyof T ] : T [ K ] }
454+ export type OmitKey < T , K extends keyof T > = { [ P in U . Exclude < keyof T , K > ] : T [ P ] }
443455}
444456
445457export namespace A {
0 commit comments