Skip to content

Instantly share code, notes, and snippets.

@brucou
Last active June 5, 2019 10:17
Show Gist options
  • Save brucou/566b6dcdf8d4ecb6e49da62999860511 to your computer and use it in GitHub Desktop.
Save brucou/566b6dcdf8d4ecb6e49da62999860511 to your computer and use it in GitHub Desktop.
robust-user-interfaces-with-state-machines - for medium
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