Skip to content

Instantly share code, notes, and snippets.

@barucAlmaguer
Created January 15, 2020 23:51
Show Gist options
  • Save barucAlmaguer/9a806736d7438265eaede54c709eeea6 to your computer and use it in GitHub Desktop.
Save barucAlmaguer/9a806736d7438265eaede54c709eeea6 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const updateContext = (prop, eventPath) => {
let obj = {}
obj[prop] = (context, event) => {
return get(event, eventPath)
}
return assign(obj)
}
const MockPlanData = {data: {coilPlan: {}}}
const updateJobContext = updateContext('job', 'job')
const setCsvContext = updateContext('csv', 'csv')
const setErrorContext = updateContext('error', 'error')
const setCoilPlanContext = updateContext('coilPlan', 'response.data.coilPlan')
const setBalances = updateContext('balances', 'balances')
const setStartAt = updateContext('startAt', 'startAt')
const setCoilsPath = updateContext('coilsPath', 'coilsPath')
const setRailsPath = updateContext('railsPath', 'railsPath')
// const setCoilPlanIdContext = updateContext('job.output.output', 'coilPlanId')
const setCoilPlanIdContext = assign({
coilPlanId: (context, event) => {
const jobOutput = event.job.output
return jobOutput.output || null
}
})
const CoilScheduleMachine = Machine({
id: 'CoilSchedule',
initial: 'idle',
context: {
job: {},
csv: [],
balances: [],
startAt: null,
coilsPath: null,
railsPath: null,
errors: [],
coilPlanId: null,
// coilPlan: { id: 1 }
coilPlan: MockPlanData.data.coilPlan // ! Debugging offline purposes
},
states: {
idle: {
on: {
SET_INPUTS: { target: 'setting_inputs' },
VIEW_PLAN: {
target: 'fetching_plan',
actions: ['setCoilPlanIdContext']
},
}
},
setting_inputs: {
on: {
START_PLANNER: {
target: 'starting_planner',
actions: [
'setBalances',
'setStartAt',
'setCoilsPath',
'setRailsPath'
]
},
RESET: 'idle'
}
},
starting_planner: {
on: {
RESOLVE: {
target: 'planning_coil_sequence',
actions: ['updateJobContext']
},
REJECT: 'failure_planning'
}
},
planning_coil_sequence: {
on: {
SUCCESS: {
target: 'fetching_plan',
actions: ['updateJobContext', 'setCoilPlanIdContext']
},
UPDATE: {
target: 'planning_coil_sequence',
actions: ['updateJobContext']
},
ERROR: 'failure_planning'
}
},
fetching_plan: {
on: {
RESOLVE: {
target: 'viewing_plan',
actions: ['setCoilPlanContext']
},
REJECT: 'failure_viewing_plan'
}
},
viewing_plan: {
// type: 'final', // ! final-ish
on: {
RESET: 'idle'
}
},
failure_planning: {
on: {
RESET: 'idle',
RETRY_SETTING_INPUTS: 'setting_inputs',
VIEW_PLAN: {
target: 'fetching_plan',
actions: ['setCoilPlanIdContext']
}
}
},
failure_viewing_plan: {
on: {
RESET: 'idle',
RETRY_SETTING_INPUTS: 'setting_inputs',
RETRY_VIEW_PLAN: {
target: 'fetching_plan',
actions: ['setCoilPlanIdContext']
}
}
}
}
}, {
actions: {
updateJobContext,
setCsvContext,
setErrorContext,
// showCsvError,
setCoilPlanContext,
setCoilPlanIdContext,
// * Plan inputs:
setBalances,
setStartAt,
setCoilsPath,
setRailsPath
},
guards: {}
})
const CoilSheduleMachine2 = Machine({
id: 'CoilSchedule2',
context: {
job: {},
csv: [],
balances: [],
startAt: null,
coilsPath: null,
railsPath: null,
errors: [],
coilPlanId: null,
coilPlan: MockPlanData.data.coilPlan // ! Debugging offline purposes
},
initial: 'idle',
states: {
idle: {
on: {
SET_INPUTS: { target: 'setting_inputs' },
VIEW_PLAN: {
target: 'viewing_plan',
actions: ['setCoilPlanIdContext']
},
}
},
setting_inputs: {
on: {
START_PLANNER: {
target: 'planning',
actions: [
'setBalances',
'setStartAt',
'setCoilsPath',
'setRailsPath'
]
},
RESET: 'idle'
}
},
planning: {
initial: 'starting_planner',
states: {
starting_planner: {},
planning: {},
success: { type: 'final'},
error: { type: 'final'}
}
},
viewing_plan: {
initial: 'fetching_plan',
states: {
fetching_plan: {},
viewing_coil_plan: {},
viewing_rail_plan: {},
no_plan_found: {
on: {
RESET: 'idle'
}
},
}
},
},
}, {
actions:{
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment