Last active
June 17, 2020 21:23
-
-
Save brookslybrand/c33367d493886a6f93e149c530ce8cb1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
const INITIAL_CONDITIONS_FAILED = 'INITIAL_CONDITIONS_FAILED' | |
const INITIAL_CONDITIONS_MET = 'INITIAL_CONDITIONS_MET' | |
const CHECK_IF_SCHEDULE_IS_VALID = 'CHECK_IF_SCHEDULE_IS_VALID' | |
const UPDATE_SCHEDULE_ID = 'UPDATE_SCHEDULE_ID' | |
// visualization of the state machine: https://xstate.js.org/viz/?gist=c33367d493886a6f93e149c530ce8cb1 | |
const startOptimizationMachine = Machine( | |
{ | |
id: 'startOptimizationMachine', | |
initial: 'disabled', | |
context: { | |
errorMessage: null, | |
scheduleId: null | |
}, | |
on: { | |
[INITIAL_CONDITIONS_FAILED]: 'disabled', | |
[UPDATE_SCHEDULE_ID]: { | |
actions: ['updateScheduleId'] | |
} | |
}, | |
states: { | |
disabled: { | |
entry: 'resetErrorMessage', | |
on: { | |
[INITIAL_CONDITIONS_MET]: 'enabled' | |
} | |
}, | |
enabled: { | |
initial: 'valid', | |
states: { | |
valid: { | |
entry: 'resetErrorMessage' | |
}, | |
invalid: {} | |
}, | |
on: { | |
[CHECK_IF_SCHEDULE_IS_VALID]: { | |
target: 'checking', | |
actions: ['resetErrorMessage'] | |
} | |
} | |
}, | |
checking: { | |
invoke: { | |
src: 'isValid', | |
onDone: 'uploading', | |
onError: { | |
target: 'enabled.invalid', | |
actions: ['setErrorMessage'] | |
} | |
} | |
}, | |
uploading: { | |
invoke: { | |
src: 'startOptimization', | |
onDone: { | |
target: 'queued', | |
actions: ['setQueuedScheduleState'] | |
}, | |
onError: { | |
target: 'enabled.invalid', | |
actions: ['setErrorMessage'] | |
} | |
} | |
}, | |
queued: { | |
type: 'final' | |
} | |
} | |
}, | |
{ | |
actions: { | |
resetErrorMessage: assign(ctx => { | |
ctx.errorMessage = null | |
}), | |
setErrorMessage: assign((ctx, e) => { | |
ctx.errorMessage = e.data | |
}), | |
updateScheduleId: assign((ctx, e) => { | |
ctx.scheduleId = e.scheduleId | |
}), | |
setQueuedScheduleState: () => { | |
throw new Error('setQueuedScheduleState must be added in `useMachine`') | |
} | |
}, | |
services: { | |
isValid: async () => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve() | |
// reject( | |
// `Configuration is invalid. Please review inputs. Issue may be with products unused or used in a circular manner.` | |
// ) | |
}, 1000) | |
}) | |
}, | |
startOptimization: async () => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve() | |
// reject( | |
// `Something went wrong. Try running the optimization again. If the issue persists reach out to Business Laboratory.` | |
// ) | |
}, 1000) | |
}) | |
} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment