Skip to content

Instantly share code, notes, and snippets.

@SuperOleg39
Created October 26, 2018 15:24
Show Gist options
  • Select an option

  • Save SuperOleg39/44d24fdd23d1b4de17bcfab5d003a2fd to your computer and use it in GitHub Desktop.

Select an option

Save SuperOleg39/44d24fdd23d1b4de17bcfab5d003a2fd to your computer and use it in GitHub Desktop.
enum States {
Empty = 'empty',
Liked = 'liked',
Disliked = 'disliked'
}
enum Actions {
Like = 'LIKE_ACTION',
Dislike = 'DISLIKE_ACTION'
}
const state = {
currentState: States.Disliked,
states: {
[States.Empty]: {
on: {
[Actions.Like]: States.Liked,
[Actions.Dislike]: States.Disliked
}
},
[States.Liked]: {
on: {
[Actions.Like]: States.Empty,
[Actions.Dislike]: States.Disliked
}
},
[States.Disliked]: {
on: {
[Actions.Like]: States.Liked,
[Actions.Dislike]: States.Empty
}
}
}
};
const transition = (action: Actions) => {
const nextState = state.states[state.currentState].on[action];
if (nextState) {
state.currentState = nextState;
}
};
transition(Actions.Like);
transition(Actions.Like);
transition(Actions.Like);
transition(Actions.Dislike);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment