Created
October 26, 2018 15:24
-
-
Save SuperOleg39/44d24fdd23d1b4de17bcfab5d003a2fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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