-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
881 lines (762 loc) · 28.6 KB
/
index.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
import { EventEmitter } from "./src/event-emitter";
import { FollowHandler } from "./src/follow-handler";
import { BaseMessages } from "./src/base-messages";
export { EventEmitter, BaseMessages };
/*
* State creation
* =================================================================================================
*/
type BuildArgs<
Next extends string,
M extends BaseMessages,
Props extends {},
C extends Children<Props, M>,
ReturnedProps extends {},
> = {
children?: C,
messages: MessageFn<Next, M, Props, ReturnedProps>,
stop?: () => any,
props?: ReturnedProps,
};
type MessageFn<
Next extends string,
M extends BaseMessages,
Props extends {},
ReturnedProps extends {},
> = (
goto: Goto<Next, Props>,
set: MaybeUpdateProps<Props>,
forward: MaybeUpdateProps<ReturnedProps>,
) => MessageDispatcher<M>;
type MaybeUpdateProps<Props extends {}> = IfEquals<Props, {}> extends true ?
null : UpdateProps<Props>;
export class DispatchBuilder<
Next extends string,
M extends BaseMessages,
Props extends {},
ParentMessages extends BaseMessages,
CurrentMiddleware extends Middleware<Next, any, Props, ParentMessages>,
> {
readonly follow = new FollowHandler();
private readonly middleware: Array<StateDispatcher<Next, M, Props, ParentMessages, any, any>>;
readonly props: Props & MiddlewareProps<CurrentMiddleware>;
constructor(
private readonly machine: Machine<any, any, any, any, any>,
props: Props,
readonly parent: Parent<ParentMessages>,
middleware: CurrentMiddleware,
private readonly friend: FriendMethods<Props>,
) {
this.middleware = Object.values(middleware || {}).map(buildFn => {
return buildFn(machine, props, parent, null as any, friend)
});
const middlewareProps: Partial<MiddlewareProps<CurrentMiddleware>> = {};
for(const dispatcher of this.middleware) {
if(!dispatcher.returnedProps) continue;
dispatcher.events.on("forward", (props) => {
mergeProps(this.props, props);
});
for(const key in dispatcher.returnedProps) {
//@ts-ignore
middlewareProps[key] = dispatcher.returnedProps[key];
}
}
this.props = {
...props,
...middlewareProps,
} as Props & MiddlewareProps<CurrentMiddleware>;
}
/*
* What is the point of these methods? Why not return the raw BuildArgs, which would be easier to
* manipulate programmatically since they're just ordinary hashes?
*
* The reason for these is to enforce type safety. The TypeScript compiler will enforce that the
* hashes you return have *at least* the expected keys, but won't enforce that extra keys are
* errors. Since most of the type safety boundaries in this library are more or less "you typed
* the same thing over here as you typed over there," not checking for extra keys is a pretty bad
* error: it means that typos typecheck, and that if you have e.g. longMispelledName instead of
* longMisspelledName as a message handler your code will silently fail. Forcing you to call
* .build({ ... }) ensures you don't miss keys (and if we don't make .build({ ... }) return
* special objects, you could forget to call it and instead return the raw hashes, which would
* once again potentially silently fail).
*
* IMO, making typos fail typechecking is more useful than making it super easy to manipulate the
* arguments programmatically. We've added explicit support for middleware, which accomplishes
* much of the same tasks, rather than making it simple-but-error-prone to manipulate the output
* of the builder functions.
*/
build(): StateDispatcher<Next, M, Props, ParentMessages, {}, {}>;
build<C extends Children<Props, M>, ReturnedProps extends {}>(
args: BuildArgs<Next, M, Props, C, ReturnedProps>
): StateDispatcher<Next, M, Props, ParentMessages, C, ReturnedProps>;
build<
C extends Children<Props, M>, ReturnedProps extends {}
>(args?: BuildArgs<Next, M, Props, C, ReturnedProps>) {
if(args) {
return new StateDispatcher<Next, M, Props, ParentMessages, C, ReturnedProps>(
args, this.follow, this.machine, this.middleware, this.props, this.friend, this
);
}
return new StateDispatcher(
{ messages: () => this.messages({} as M) },
this.follow,
this.machine,
this.middleware,
this.props,
this.friend,
this,
);
}
messages(messages: M): MessageDispatcher<M> {
return new MessageDispatcher(messages);
}
msg(messages: M): MessageDispatcher<M> {
return this.messages(messages);
}
dispatch<Name extends keyof M>(name: Name, ...data: Params<M[Name]>) {
this.machine.dispatch(name, ...data);
}
child<
ChildM extends BaseMessages = {},
ChildProps extends Props = Props
>(): MachineBuilder<ChildM, ChildProps, Parent<M>, Props> {
return new MachineBuilder();
}
}
type Children<Props extends {}, ParentMessages extends BaseMessages> = {
[key: string]: Machine<any, any, any, Props, Parent<ParentMessages>>,
};
export class StateBuilder<
Next extends string,
M extends BaseMessages = {},
Props extends {} = {},
ParentMessages extends BaseMessages = {},
CurrentMiddleware extends Middleware<Next, any, Props, ParentMessages> = {},
> {
constructor(
private readonly _middleware: CurrentMiddleware = {} as CurrentMiddleware
) {}
// Override for empty build constructor: ultra shorthand syntax
build(): DispatchBuildFn<
Next, {}, {}, StateDispatcher<Next, {}, {}, ParentMessages, never, {}>, ParentMessages, {}
>;
// Override for actually providing a real builder function
build<ReturnedProps extends {}, Dispatcher extends StateDispatcher<
Next,
MessagesForDispatch<M, CurrentMiddleware>,
StateProps<Props, CurrentMiddleware>,
ParentMessages,
any,
ReturnedProps
>>(
curryBuildFn: (
builder: DispatchBuilder<
Next, MessagesForDispatch<M, CurrentMiddleware>, Props, ParentMessages, CurrentMiddleware
>
) => Dispatcher
): DispatchBuildFn<
Next,
MessagesForDispatch<M, CurrentMiddleware>,
StateProps<Props, CurrentMiddleware>,
Dispatcher,
ParentMessages,
ReturnedProps
>;
// The implementation for the two overrides
build<ReturnedProps extends {}, Dispatcher extends StateDispatcher<
Next,
MessagesForDispatch<M, CurrentMiddleware>,
StateProps<Props, CurrentMiddleware>,
ParentMessages,
any,
ReturnedProps
>>(
curryBuildFn?: (
builder: DispatchBuilder<
Next, MessagesForDispatch<M, CurrentMiddleware>, Props, ParentMessages, CurrentMiddleware
>
) => Dispatcher
) {
return (machine: any, props: any, parent: any, _: any, friend: any) => {
if(!curryBuildFn) {
curryBuildFn = (
(state: DispatchBuilder<Next, any, Props, ParentMessages, CurrentMiddleware>) => {
return state.build({ messages: () => state.messages({}) });
}) as (
(builder: DispatchBuilder<
Next,
MessagesForDispatch<M, CurrentMiddleware>,
Props,
ParentMessages,
CurrentMiddleware
>) => Dispatcher
);
}
return curryBuildFn(
new DispatchBuilder<
Next, MessagesForDispatch<M, CurrentMiddleware>, Props, ParentMessages, CurrentMiddleware
>(
machine, props, parent, this._middleware, friend
)
);
};
}
middleware<NewMiddleware extends Middleware<any, any, Props, ParentMessages>>(
middleware: CheckMiddlewareProps<
CheckMiddlewareVariance<
MiddlewareNext<NewMiddleware>,
Next,
NoDuplicateKeys<NewMiddleware, CurrentMiddleware>
>,
Props
>
): StateBuilder<
Next,
M,
Props,
ParentMessages,
CurrentMiddleware & NewMiddleware
> {
//@ts-ignore
return new StateBuilder({
...this._middleware,
...middleware,
});
}
}
// Types to check middleware variance
type MiddlewareMessages<T> = T extends Middleware<any, infer M, any, any> ?
UnionToIntersection<M> : never;
type MiddlewareNext<T> = T extends Middleware<infer N, any, any, any> ? N : never;
type MessagesForDispatch<M extends BaseMessages, CurrentMiddleware> =
IfEquals<CurrentMiddleware, {}> extends true ? M :
Omit<M, keyof MiddlewareMessages<CurrentMiddleware>>
& OptionalOverride<MiddlewareMessages<CurrentMiddleware>, M>;
type CheckMiddlewareVariance<MiddlewareNext extends string, Next extends string, M> =
[MiddlewareNext] extends [never] ? M :
IfEquals<MiddlewareNext, Next> extends true ? M :
IsStringUnionSubtype<MiddlewareNext, Next, M>;
// Types to check middleware props
type CheckMiddlewareProps<NewMiddleware extends Middleware<any, any, any, any>, Props extends {}> =
IfEquals<NewMiddleware, {}> extends true ? NewMiddleware :
Props extends MiddlewareProps<NewMiddleware> ? NewMiddleware : never;
type BuildFnProps<T> = T extends DispatchBuildFn<any, any, any, infer D, any, any> ?
D extends StateDispatcher<any, any, any, any, any, infer RP> ?
RP : never : never;
type MiddlewarePropsUnion<T extends Middleware<any, any, any, any>> = {
[K in keyof T]: BuildFnProps<T[K]>;
}[keyof T];
type UnionToIntersection<U> =
(U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never
export type MiddlewareProps<T extends Middleware<any, any, any, any>> =
UnionToIntersection<MiddlewarePropsUnion<T>>;
// Type to remove props from a set if they're defined in the middleware
type StateProps<Props extends {}, CurrentMiddleware extends Middleware<any, any, any, any>> =
IfEquals<CurrentMiddleware, {}> extends true ? Props :
Omit<Props, keyof MiddlewareProps<CurrentMiddleware>>;
// Normally checking whether SmallerUnion extends BiggerUnion will return true *regardless of which
// is larger.* You can hack around this via the behavior of Exclude, which doesn't suffer from this
// flaw.
type IsStringUnionSubtype<SmallerUnion, BiggerUnion, RetVal> = IfNever<
Exclude<BiggerUnion, SmallerUnion>
> extends false ? RetVal : never;
// Checking strict string union *subtyping* doesn't cover the case in which two string types are
// exactly equal. If they are, you probably also want to have it succeed.
type IfEquals<T, U, Y=true, N=false> =
(<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? Y : N;
// Checking for never extension seems broken. A workaround is wrapping with an array and checking
// for never arrays
type IfNever<T> = [T] extends [never] ? true : false;
type Middleware<
Next extends string,
M extends BaseMessages,
Props extends {},
ParentMessages extends BaseMessages,
> = {
[key: string]: DispatchBuildFn<
Next,
M,
Props,
StateDispatcher<Next, M, Props, ParentMessages, any, any>,
ParentMessages,
any
>
};
type NoDuplicateKeys<T, U> = IfNever<U> extends true ? T :
IfNever<T> extends true ? T : keyof T extends keyof U ? never : T;
type OptionalOverride<Middleware extends {}, Overrides extends {}> =
{} extends Middleware ? {} : {
[K in keyof Middleware]?: K extends keyof Overrides ? Overrides[K] : never;
};
export function transition<
Next extends string = never,
M extends BaseMessages = {},
Props extends {} = {},
Parent extends BaseMessages = {},
>(): StateBuilder<Next, M, Props, Parent> {
return new StateBuilder();
}
// "Friend methods" a la C++
// Since JS/TS doesn't have "friend" types -- e.g. types that are allowed to call other types'
// private methods -- we pass in a hash of friendly methods that we're letting someone call, even
// though on our end these are private methods. For example, we don't want to expose prop updating
// outside of message handlers, but we want message handlers to be able to update props on the
// machine -- so the machine generates a friend hash and passes it through to the builders.
type UpdateProps<Props extends {}> = (props: Partial<Props>) => void;
type FriendMethods<Props extends {}> = {
updateProps: UpdateProps<Props>,
};
// Complex type that we build for constructing new states. It takes one fake param (the last one),
// solely used to force ParentMessages to be contravariant. We always pass null to that param and
// cast to any.
export type DispatchBuildFn<
Next extends string,
M extends BaseMessages,
Props extends {},
Dispatcher extends StateDispatcher<Next, M, Props, ParentMessages, any, ReturnedProps>,
ParentMessages extends BaseMessages,
ReturnedProps extends {}
> = (
machine: Machine<any, any, any, any, any>,
props: Props,
parent: Parent<NonNullable<ParentMessages>>,
_: ParentMessages,
friend: FriendMethods<Props>,
) => Dispatcher;
export class Parent<M extends BaseMessages> {
constructor(private readonly dispatcher: StateDispatcher<any, M, any, any, any, any>) {}
dispatch<Name extends keyof M>(name: Name, ...data: Params<M[Name]>) {
this.dispatcher.dispatch(
name,
...data
);
}
}
// Message creation
// -------------------------------------------------------------------------------------------------
function gotoBuilder<Next extends string, Props extends {}>(
machine: Machine<any, any, any, any, any>
): Goto<Next, Props> {
return function goto(next, updateProps?) {
machine.force(next, updateProps);
};
}
export type Goto<Next extends string, Props extends {}> = (
next: Next, updateProps?: Partial<Props>
) => void;
/*
* Message dispatching for states
* =================================================================================================
*/
export class MessageDispatcher<M extends BaseMessages> {
constructor(
private readonly messages: M
) {}
dispatch<Name extends keyof M>(name: Name, ...data: Params<M[Name]>) {
const handler = this.messages[name];
if(handler) handler(...data as any[]);
}
}
type PropsForwarded<ReturnedProps extends {}> = {
forward: Partial<ReturnedProps>,
};
export class StateDispatcher<
Next extends string,
M extends BaseMessages,
P extends {},
ParentMessages extends BaseMessages,
C extends Children<P, M>,
ReturnedProps extends {}
> {
readonly hasChildren: boolean; // dumb micro optimization for cpu branch predictor
readonly children: Children<P, M>;
readonly returnedProps?: ReturnedProps & Partial<P>;
readonly props: Partial<P>; // variance rules are annoying when combining middleware
readonly events = new EventEmitter<PropsForwarded<ReturnedProps>>();
private readonly _stop: (() => any) | undefined;
private readonly messages: MessageDispatcher<M>;
// When a middleware or state calls goto(), eventually stop() will be called, and this state will
// permanently die. Future states are new instantiations of this class. We can track whether this
// state is dead by tracking stop() calls, which allows short-circuiting if a middleware calls
// goto() somewhere in the chain.
private _dead = false;
constructor(
args: BuildArgs<Next, M, P, C, ReturnedProps>,
private readonly follow: FollowHandler,
machine: Machine<any, any, any, any, any>,
private readonly middleware: Array<StateDispatcher<Next, M, P, ParentMessages, any, any>>,
props: P,
friend: FriendMethods<P>,
readonly builder: DispatchBuilder<any, any, any, any, any>
) {
this.children = args.children || {};
this.hasChildren = !!args.children;
this.messages = args.messages(
gotoBuilder(machine),
friend.updateProps as MaybeUpdateProps<P>,
((returned) => {
if(this.returnedProps) mergeProps(this.returnedProps, returned);
this.events.emit("forward", returned);
}) as MaybeUpdateProps<ReturnedProps>,
);
this._stop = args.stop;
this.returnedProps = args.props;
this.props = props;
}
dispatch<Name extends keyof M>(
name: Name,
...data: Params<M[Name]>
) {
// no-op if goto got called before you did
if(this._dead) return;
if(this.hasChildren) {
for(const key in this.children) {
const child = this.children[key];
child.dispatch(name, ...data);
}
}
for(let i = 0; i < this.middleware.length; i++) {
this.middleware[i].dispatch(name, ...data);
// no-op the rest of this function if a middleware called goto
if(this._dead) return;
}
this.messages.dispatch(name, ...data);
}
stop() {
this._dead = true;
if(this.hasChildren) {
for(const key in this.children) {
const child = this.children[key];
child.stop();
}
}
for(let i = 0; i < this.middleware.length; i++) {
this.middleware[i].stop();
}
if(this._stop) this._stop();
this.follow.clear();
}
}
type Params<T> = T extends (...args: any) => any ? Parameters<T> : [];
/*
* Flyweights for event registration
* =================================================================================================
*/
export type StateEvents<Props extends {}> = {
start: Props,
stop: Props,
};
export class MachineFlyweight<Props extends {}, M extends Machine<any, any, any, any, any>> {
readonly dispatchers: {
[K in M["builders"]]?: DispatcherFlyweight<Props, ReturnType<M["builders"][K]>>;
} = {};
events<Name extends keyof M["builders"]>(
name: Name
): DispatcherFlyweight<Props, ReturnType<M["builders"][Name]>> {
return upsert(this.dispatchers, name, () => new DispatcherFlyweight());
}
}
export type GetChildren<T> = T extends StateDispatcher<any, any, any, any, infer C, any> ? C : never;
export class DispatcherFlyweight<
Props extends {},
Dispatcher extends StateDispatcher<any, any, Props, any, any, any>
> extends EventEmitter<StateEvents<Props>> {
readonly children: {
[K in keyof GetChildren<Dispatcher>]?: MachineFlyweight<Props, GetChildren<Dispatcher>[K]>;
} = {};
child<Name extends keyof GetChildren<Dispatcher>>(name: Name) {
return upsert(this.children, name, () => new MachineFlyweight());
}
}
function upsert<Hash extends {}, Key extends keyof Hash>(
hash: Hash, key: Key, create: () => NonNullable<Hash[Key]>
): NonNullable<Hash[Key]> {
let val = hash[key];
if(val) return val;
const createdVal = create();
hash[key] = createdVal;
return createdVal;
}
/*
* Machines
* =================================================================================================
*/
type BuilderMap<
M extends BaseMessages,
AllTransitions extends string,
Props extends {},
ParentType extends Parent<any> | null
> = {
[K in AllTransitions]: DispatchBuildFn<any, Partial<M>, Props, any, MessagesFrom<ParentType>, any>;
};
export type MessagesFrom<P> = P extends Parent<infer M> ? M : {};
// The end goal of this is the final accessor: a way to figure out what keys need to be in the state
// class map you pass into the machine constructor. Otherwise, the class map won't ensure that your
// map is exhaustive; that is, you could have asked for transitions to states that don't exist in
// the map.
type NextStateOf<T> = T extends DispatchBuildFn<infer Next, any, any, any, any, any> ? Next : never;
export type BuilderMapOf<M> = M extends Machine<any, infer BM, any, any, any> ? BM : never;
// Grab the state transition names from the builder map. This returns whatever transitions are in
// the keys; it doesn't yet tell you which transitions are asked for
export type TransitionNamesOf<M> = M extends BuilderMap<any, infer T, any, any> ? T : never;
// Get exactly the list of transitions requested from the builder map
export type LoadPreciseTransitions<BM extends BuilderMap<any, any, any, any>> = NextStateOf<
BM[TransitionNamesOf<BM>]
>;
export type FullySpecifiedBuilderMap<BM extends BuilderMap<any, any, any, any>> = {
[K in LoadPreciseTransitions<BM>]: DispatchBuildFn<any, any, any, any, any, any>;
}
export type RestrictParentProps<StaticProps extends {}, ParentProps extends {}> = {
[K in keyof StaticProps]: K extends keyof ParentProps ? never : StaticProps[K];
};
type MachineArgs<
M extends BaseMessages,
StaticProps extends {},
B extends BuilderMap<M, any, any, any>,
ParentProps extends {},
> = {
initial: keyof B & string,
states: B & FullySpecifiedBuilderMap<B>
props: StaticProps & RestrictParentProps<StaticProps, ParentProps>
};
export class Machine<
M extends BaseMessages,
B extends BuilderMap<M, any, any, ParentType>,
StaticProps extends {},
DynamicProps extends {},
ParentType extends Parent<any> | null
> {
readonly builders: B;
private readonly _dispatcherEventMap: {
[K in keyof B]?: DispatcherFlyweight<StaticProps & DynamicProps, ReturnType<B[K]>>
} = {};
private _current: StateDispatcher<
any, Partial<M>, StaticProps & DynamicProps, any, any, any
> | null = null;
private _currentName: keyof B & string;
private _everRan = false;
private _running = false;
private _staticProps: StaticProps;
private _props: (DynamicProps & StaticProps) | null = null;
private readonly _initial: keyof B & string;
protected _parent: ParentType | null = null;
private _dispatchQueue: Array<{ name: keyof M, data: any[] }> = [];
private _isStartingNewState = false;
constructor(args: MachineArgs<M, StaticProps, B, any>) {
this._initial = this._currentName = args.initial;
this._staticProps = args.props;
this.builders = args.states;
}
running() {
return this._running;
}
props(): StaticProps & DynamicProps {
if(!this._props) throw new Error("Internal error: props are null");
return this._props;
}
goto(next: keyof B & string, updateProps?: Partial<StaticProps & DynamicProps>) {
// Boilerplate safety
if(next === this._currentName) return;
this.force(next, updateProps);
}
force(next: keyof B & string, updateProps?: Partial<StaticProps & DynamicProps>) {
this._assertRunning();
if(!this._props) throw new Error("Internal error: props are null");
// Update props, if new ones were passed in
if(updateProps !== undefined) {
this._updateProps(updateProps);
}
this._stopCurrent();
this._createAndStart(next, this._props);
}
current() {
return this._currentName;
}
start(dynamicProps: DynamicProps) {
if(this._running) return;
this._everRan = true;
this._running = true;
const props = this._props = {
...this._staticProps,
...dynamicProps,
};
this._createAndStart(this._initial, props);
}
stop() {
// This check is necessary for stop idempotence! Otherwise subsequent calls will explode when
// dispatch asserts that the machine is running.
if(!this._running) return;
this._stopCurrent();
this._running = false;
}
dispatch<Name extends keyof M>(
name: Name,
...data: Params<M[Name]>
) {
this._assertRunning();
// If you're starting a new state, queue the dispatch. The state creation code will pick up once
// the state creation is done.
if(this._isStartingNewState) {
this._dispatchQueue.push({ name, data });
return;
}
if(!this._current) throw new Error("Internal error: _current was never initialized");
this._current.dispatch(name, ...data);
}
events<Name extends keyof B>(
name: Name
): DispatcherFlyweight<StaticProps & DynamicProps, ReturnType<B[Name]>> {
return upsert(this._dispatcherEventMap, name, () => {
return new DispatcherFlyweight();
});
}
currentEvents() {
return upsert(this._dispatcherEventMap, this._currentName, () => new DispatcherFlyweight());
}
protected _hydrate(
parent: ParentType,
events: MachineFlyweight<StaticProps & DynamicProps, any> | undefined
) {
this._parent = parent;
if(events) {
for(const k in events.dispatchers) {
const key = k as keyof (typeof events.dispatchers);
const dispatcherFly = events.dispatchers[key];
if(dispatcherFly) {
this._dispatcherEventMap[key as keyof B] = dispatcherFly;
}
}
}
}
protected _updateProps(updateProps: Partial<StaticProps & DynamicProps>) {
if(!this._props) throw new Error("Internal error: props are null");
mergeProps(this._props, updateProps);
if(!this._current) throw new Error("Internal error: _current was never initialized");
// Have to update the builder props too, sigh, in case something reads from them
mergeProps(this._current.builder.props, updateProps);
// Forward updates to children
if(this._current.hasChildren) {
for(const key in this._current.children) {
this._current.children[key]._updateProps(updateProps);
}
}
}
private _createAndStart<N extends keyof B>(name: N & string, props: StaticProps & DynamicProps) {
const stateBuilder = this.builders[name];
this._currentName = name;
this._isStartingNewState = true;
const current = this._current = stateBuilder(this, props, this._parent as any, null as any, {
updateProps: (props) => {
this._updateProps(props);
},
});
this._isStartingNewState = false;
const dispatcherEvent = this._dispatcherEventMap[name];
// Hydrate child machines
if(current.hasChildren) {
const parent = new Parent(current);
for(const key in current.children) {
const child = current.children[key];
const events = dispatcherEvent ? dispatcherEvent.children[key] : undefined;
child._hydrate(parent, events);
child.start({
// Use the dispatcher props since middleware may have added data
...current.props,
// ...But merge ours in on top, since they may have changed via set calls
...props,
});
}
}
// Dequeue from the dispatch queue if necessary
if(this._dispatchQueue.length !== 0) {
for(let i = 0; i < this._dispatchQueue.length; i++) {
const queued = this._dispatchQueue[i];
this.dispatch(queued.name, ...queued.data as any);
}
this._dispatchQueue = [];
}
if(dispatcherEvent) dispatcherEvent.emit("start", props);
}
private _stopCurrent() {
if(!this._current) throw new Error("Internal error: _current was never initialized");
if(!this._props) throw new Error("Internal error: _props was never initialized");
this._current.stop();
const dispatcherEvent = this._dispatcherEventMap[this._currentName];
if(dispatcherEvent) dispatcherEvent.emit("stop", this._props);
}
private _assertRunning() {
if(!this._everRan) throw new Error("State machine was never started");
if(!this._running) throw new Error("State machine is stopped");
}
}
function mergeProps<Props extends {}>(dest: Props, update: Partial<Props>) {
for(const k in update) {
// Dumb typecheck workarounds
const key = k as keyof Props;
dest[key] = update[key] as Props[typeof k];
}
}
/*
* Machine building helpers
* =================================================================================================
*
* Machines are a PITA to manually construct, since they've got so many type params, and you have to
* manually keep track of which ones you're passing as static vs dynamic props. The following
* creates a convenient helper function that allows you to just define Messages and Props, like the
* states do, and automatically infers StaticProps vs DynamicProps based on what you pass into the
* constructor argument.
*/
type DefinedKeys<Some> = {
[K in keyof Some]-?: Some[K] extends undefined ? never : K;
}[keyof Some];
type Rest<Full, Some extends Partial<Full>> = Omit<Full, DefinedKeys<Some>>;
type NoStaticPropsArgs<
B extends BuilderMap<any, any, any, any>,
> = {
initial: keyof B & string,
states: B & FullySpecifiedBuilderMap<B>
};
export class MachineBuilder<
M extends BaseMessages,
Props extends {},
ParentType extends Parent<any> | null,
ParentProps extends {}
> {
build<B extends BuilderMap<M, any, Props, ParentType>>(
// If there is a parent, ensure that the parent's props are a subtype of this machine's props.
// If they aren't, the user needs to specify the extra props as static props using the other
// override build function
args: ParentType extends null ? NoStaticPropsArgs<B> : (
ParentProps extends Props ? NoStaticPropsArgs<B> : never
)
): Machine<M, B, {}, Props, ParentType>;
build<
B extends BuilderMap<M, any, Props, ParentType>,
StaticProps extends Partial<Props>
>(
args: MachineArgs<M, StaticProps, B, ParentProps>
): Machine<M, B, StaticProps, Rest<Props, StaticProps>, ParentType>;
build<
B extends BuilderMap<M, any, Props, ParentType>,
StaticProps extends Partial<Props>
>(args: NoStaticPropsArgs<B> | MachineArgs<M, StaticProps, B, ParentProps>) {
if(hasStaticProps(args)) return new Machine(args);
return new Machine({
...args,
props: {},
});
}
}
function hasStaticProps<
N extends NoStaticPropsArgs<any>,
M extends MachineArgs<any, any, any, any>
>(args: N | M): args is M {
return (args as MachineArgs<any, any, any, any>).props !== undefined;
}
export function machine<
M extends BaseMessages = {},
Props extends {} = {}
>(): MachineBuilder<M, Props, null, {}> {
return new MachineBuilder();
}