Skip to content

Commit

Permalink
patch: rename transitionfrom to static transition
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 11, 2023
1 parent 249c599 commit f3450d6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/vest-utils/src/SimpleStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export type TStateMachine<S extends string, A extends string> = {
export type TStateMachineApi<S extends string, A extends string> = {
getState: CB<S>;
transition: (action: A, payload?: any) => void;
transitionFrom: (from: S, action: A, payload?: any) => S;
staticTransition: (from: S, action: A, payload?: any) => S;
};

export function StateMachine<S extends string, A extends string>(
machine: TStateMachine<S, A>
): TStateMachineApi<S, A> {
let state = machine.initial;

const api = { getState, transition, transitionFrom };
const api = { getState, transition, staticTransition };

return api;

Expand All @@ -32,11 +32,11 @@ export function StateMachine<S extends string, A extends string>(
}

function transition(action: A, payload?: any): S {
return (state = transitionFrom(state, action, payload));
return (state = staticTransition(state, action, payload));
}

// eslint-disable-next-line complexity
function transitionFrom(from: S, action: A, payload?: any): S {
function staticTransition(from: S, action: A, payload?: any): S {
const transitionTo =
machine.states[from]?.[action] ??
// @ts-expect-error - This is a valid state
Expand Down
30 changes: 25 additions & 5 deletions packages/vest-utils/src/__tests__/SimpleStateMachine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('SimpleStateMachine', () => {
});
});

describe('transitionFrom', () => {
describe('staticTransition', () => {
describe('When the transition is valid', () => {
it('Should return the new state', () => {
const machine = StateMachine({
Expand All @@ -314,7 +314,27 @@ describe('SimpleStateMachine', () => {
},
});
expect(machine.getState()).toBe('idle');
expect(machine.transitionFrom('idle', 'click')).toBe('loading');
expect(machine.staticTransition('idle', 'click')).toBe('loading');
});

it('Should not modify the state', () => {
const machine = StateMachine({
initial: 'idle',
states: {
error: {},
idle: {
click: 'loading',
},
loading: {
success: 'success',
error: 'error',
},
success: {},
},
});
expect(machine.getState()).toBe('idle');
machine.staticTransition('idle', 'click');
expect(machine.getState()).toBe('idle');
});
});

Expand All @@ -335,7 +355,7 @@ describe('SimpleStateMachine', () => {
},
});
expect(machine.getState()).toBe('idle');
expect(machine.transitionFrom('idle', 'finish')).toBe('idle');
expect(machine.staticTransition('idle', 'finish')).toBe('idle');
});
});

Expand All @@ -356,7 +376,7 @@ describe('SimpleStateMachine', () => {
},
});
expect(machine.getState()).toBe('idle');
expect(machine.transitionFrom('idle', 'click')).toBe('idle');
expect(machine.staticTransition('idle', 'click')).toBe('idle');
});
});

Expand All @@ -377,7 +397,7 @@ describe('SimpleStateMachine', () => {
},
});
expect(machine.getState()).toBe('idle');
expect(machine.transitionFrom('idle', 'click')).toBe('loading');
expect(machine.staticTransition('idle', 'click')).toBe('loading');
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class VestTestMutator {
status: TestStateMachineAction,
payload?: any
): void {
test.status = TestStateMachine.transitionFrom(test.status, status, payload);
test.status = TestStateMachine.staticTransition(
test.status,
status,
payload
);
}
}

2 comments on commit f3450d6

@vercel
Copy link

@vercel vercel bot commented on f3450d6 Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest.vercel.app
www.vestjs.dev
vest-ealush.vercel.app
vest-git-latest-ealush.vercel.app
vestjs.dev

@vercel
Copy link

@vercel vercel bot commented on f3450d6 Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-website.vercel.app
vest-next-ealush.vercel.app

Please sign in to comment.