Last active
June 5, 2019 10:17
-
-
Save brucou/566b6dcdf8d4ecb6e49da62999860511 to your computer and use it in GitHub Desktop.
robust-user-interfaces-with-state-machines - for medium
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
function makePromiseMachine(params) { | |
let state = { | |
control: 'pending', | |
// ... other pieces of state | |
}; | |
return function (event) { | |
const control = state.control; | |
let output; | |
switch (event){ | |
case 'approve': | |
switch (control) { | |
case 'pending': | |
// update state, update output | |
state.control = 'approved' | |
output = ... | |
break; | |
default: | |
break; | |
} | |
break; | |
case 'reject': | |
switch (control) { | |
case 'pending': | |
// update state, update output | |
state.control = 'rejected' | |
output = ... | |
break; | |
case 'approved': | |
// update state, update output | |
state.control = 'rejected' | |
output = ... | |
break; | |
default: | |
break; | |
} | |
break; | |
case 'pend': | |
(...) | |
break; | |
default: | |
break; | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment