Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Last active June 17, 2020 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookslybrand/c33367d493886a6f93e149c530ce8cb1 to your computer and use it in GitHub Desktop.
Save brookslybrand/c33367d493886a6f93e149c530ce8cb1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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