-
-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace
xstate
with a simple built-in state machine (#7460)
- Loading branch information
Showing
18 changed files
with
1,424 additions
and
1,823 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export interface StateMachineTransition< | ||
State extends StateMachineState, | ||
Effect = undefined, | ||
> { | ||
effect?: Effect; | ||
newState: State; | ||
} | ||
|
||
export interface StateMachineState { | ||
value: number | string; | ||
done?: boolean; | ||
} | ||
|
||
export interface StateMachineInput { | ||
value: number | string; | ||
} | ||
|
||
export type StateMachineTransitionMap< | ||
State extends StateMachineState, | ||
Input extends StateMachineInput, | ||
Effect = undefined, | ||
> = ( | ||
state: State, | ||
) => ( | ||
input: Input, | ||
) => StateMachineTransition<State, Effect | undefined> | undefined; | ||
|
||
export type InferStateMachineTransitions< | ||
T extends StateMachine<any, any, any>, | ||
> = T extends StateMachine<infer S, infer I, infer E> | ||
? StateMachineTransitionMap<S, I, E | undefined> | ||
: never; | ||
|
||
export class StateMachine< | ||
State extends StateMachineState, | ||
Input extends StateMachineInput, | ||
Effect = undefined, | ||
> { | ||
public constructor( | ||
initialState: State, | ||
transitions: StateMachineTransitionMap< | ||
State, | ||
Input, | ||
Effect | undefined | ||
>, | ||
) { | ||
this._initial = this._state = initialState; | ||
this.transitions = transitions; | ||
} | ||
|
||
protected transitions: StateMachineTransitionMap< | ||
State, | ||
Input, | ||
Effect | undefined | ||
>; | ||
|
||
/** Restarts the machine from the initial state */ | ||
public restart(): void { | ||
this._state = this._initial; | ||
} | ||
|
||
/** Determines the next transition to take */ | ||
public next( | ||
input: Input, | ||
): StateMachineTransition<State, Effect | undefined> | undefined { | ||
if (this._state.done) return; | ||
return this.transitions(this._state)(input); | ||
} | ||
|
||
/** Transitions the machine to the next state. This does not execute effects */ | ||
public transition(next?: State): void { | ||
// Allow some convenience by passing the transition's next state directly | ||
if (next == undefined) return; | ||
this._state = next; | ||
} | ||
|
||
private _initial: State; | ||
private _state: State; | ||
/** Returns the current state of the state machine */ | ||
public get state(): State { | ||
return this._state; | ||
} | ||
|
||
/** Returns whether this state machine is done */ | ||
public get done(): boolean { | ||
return !!this._state.done; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.