You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the feature in detail (code, mocks, or screenshots encouraged)
e.g. calling send on a machine should have typed arguments.
What type of pull request would this be?
New Feature
Provide relevant links or additional information.
Here's a start.
Some caveats:
sharing action names on multiple states fails to type
_enter/_exit arguments are unknown[]
the glob is not included in the states object
only arrow functions seem to work for state handlers
import{FiniteStateMachine,typeFSMLifecycle,typeFSMLifecycleFn,typeLifecycleFnMeta}from"runed"// this is from type festexporttypeUnionToIntersection<Union>=// `extends unknown` is always going to be the case and is used to convert the// `Union` into a [distributive conditional// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).(Unionextendsunknown ?
// The union type is used as the only argument to a function since the union// of function arguments is an intersection.(distributedUnion: Union)=>void
: // This won't happen.never)extends(// Infer the `Intersection` type since TypeScript represents the positional// arguments of unions of functions as an intersection of the union.(mergedIntersection: infer Intersection)=>void) ?
// The `& Union` is to allow indexing by the resulting typeIntersection&Union
: neverexporttypeAnyTransition={[sinstring]: {_enter?: (...args: any[])=>void_exit?: (...args: any[])=>void}&{[Kinstring]: string|((...args: any[])=>string|void)}}exporttypeState<TextendsAnyTransition>=string&keyofTexporttypeHandler<TextendsAnyTransition>=UnionToIntersection<T[State<T>]>exporttypeActionHandler<TextendsAnyTransition>={[KinExclude<keyofHandler<T>,FSMLifecycle>]: Handler<T>[K]}exporttypeEvent<TextendsAnyTransition>=string&keyofActionHandler<T>exporttypeEventArgs<TextendsAnyTransition,EextendsEvent<T>>=ActionHandler<T>[E]extends(...args: any[])=>any ? Parameters<ActionHandler<T>[E]>
: unknown[]exporttypeStateHandler<TextendsAnyTransition,SextendsState<T>>={[EinkeyofT[S]]: T[S][E]extendsState<T> ?
T[S][E]// string action
: T[S][E]extends(...args: any[])=>State<T>|void ?
EextendsFSMLifecycle ?
FSMLifecycleFn<State<T>,Event<T>>// lifecycle function
: T[S][E]// function action
: never}exporttypeFSMLifecycleFns<TextendsAnyTransition>={[KinFSMLifecycle]?: FSMLifecycleFn<State<T>,Event<T>>}exporttypeSomeTransition<CextendsAnyTransition>={[SinState<C>]: StateHandler<C,S>}exportdeclareclassTypedFiniteStateMachine<TextendsAnyTransition>{/** Triggers a new event and returns the new state. */send<EextendsEvent<T>>(event: E, ...args: EventArgs<T,E>): State<T>|void/** Debounces the triggering of an event. */debounce<EextendsEvent<T>>(wait: number|undefined,event: E,
...args: EventArgs<T,E>): Promise<State<T>>/** The current state. */getcurrent(): State<T>}exportconstcreateFiniteStateMachine=<TextendsAnyTransition>(initial: State<T>,states: SomeTransition<T>,glob: FSMLifecycleFns<T>={}): TypedFiniteStateMachine<T>=>newFiniteStateMachine(initial,// @ts-expect-error{ ...states,"*": glob})// exampletypeMyState="on"|"off"typeMyAction="turnOff"|"turnOn"|"disable"|"enable"typeMyMeta=LifecycleFnMeta<MyState,MyAction>constmachine=createFiniteStateMachine("off",{on: {turnOff: "off",_enter: (meta: MyMeta)=>{console.log({ meta })},_exit: (meta: MyMeta)=>{console.log({ meta })},disable: (number: number)=>{console.log({ number })return"off"asconst}},off: {turnOn: "on",enable: ()=>{return}}})machine.send("enable")machine.send("disable",1)machine.send("turnOn")machine.send("turnOff")
The text was updated successfully, but these errors were encountered:
Describe the feature in detail (code, mocks, or screenshots encouraged)
e.g. calling
send
on a machine should have typed arguments.What type of pull request would this be?
New Feature
Provide relevant links or additional information.
Here's a start.
Some caveats:
unknown[]
The text was updated successfully, but these errors were encountered: