Skip to content

Commit

Permalink
feat: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Jun 10, 2024
1 parent ac00462 commit 49ca69f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/js/hooks/useStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export interface Send<E extends string> {
(event: E): void;
}

export interface Guard<C, E extends string> {
(event: E, context: C): boolean;
}

interface TransitionAction<E extends string> {
type: ActionType.Transition;
event: E;
Expand All @@ -44,11 +40,15 @@ export interface State<C, S extends string, E extends string> {
readonly nextEvents: E[];
}

export interface Guard<C, S extends string, E extends string> {
(state: S, event: E, context: C): boolean;
}

export interface Options<C, S extends string, E extends string> {
initial: S;
verbose?: boolean;
states: {
[key in S]: StateOptions<C, S, E>;
[K in S]: StateOptions<C, S, E>;
};
}

Expand All @@ -65,7 +65,7 @@ export interface StateOptions<C, S extends string, E extends string> {

type Action<C, E extends string> = UpdateAction<C> | TransitionAction<E>;

type Transition<C, S extends string, E extends string> = S | { target: S; guard?: Guard<C, E> };
type Transition<C, S extends string, E extends string> = S | { target: S; guard?: Guard<C, S, E> };

type UseStateMachine<C, S extends string, E extends string> = [state: State<C, S, E>, send: Send<E>, update: Update<C>];

Expand Down Expand Up @@ -125,7 +125,7 @@ function getReducer<C, S extends string, E extends string>(options: Options<C, S
const { guard } = nextState;

// If there are guards, invoke them and return early if the transition is denied.
if (guard && !guard(event, context)) {
if (guard && !guard(value, event, context)) {
if (verbose) {
debug(`Transition from "${value}" to "${target}" denied by guard`);
}
Expand Down

0 comments on commit 49ca69f

Please sign in to comment.