Skip to content

Instantly share code, notes, and snippets.

@plugboy
Created May 2, 2020 10:12
Show Gist options
  • Save plugboy/0f4b4e1d441defa0fac5a3256fd122d9 to your computer and use it in GitHub Desktop.
Save plugboy/0f4b4e1d441defa0fac5a3256fd122d9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// State Machine for Charting
const stateMachine = Machine(
{
id: 'stateMachine',
initial: 'idle',
context: {
file: undefined,
svg: undefined,
error: undefined
},
states: {
idle: {
on: {
click: { target: 'idle', actions: 'startFileDialog' },
change: { target: 'filePicked', cond: 'isFileListNotEmpty', actions: 'saveFileContext' }
}
},
filePicked: {
on: {
'': [
{ target: 'readFile', cond: 'isSvgFile'},
{ target: 'wrongFileType'}
]
}
},
readFile: {
exit: 'resetFileChange',
invoke: {
id: 'readFile',
src: 'readFile',
onDone: [
{ target: 'fileLoaded', cond: 'shouldSaveSvgContext', actions: 'saveSvgContext' },
{ target: 'idle', cond: 'isSvgContextUndefined' },
{ target: 'fileLoaded'},
],
onError: { target: 'loadFailed', actions: 'saveErrorContext' },
}
},
wrongFileType: {
entry: 'showFileTypeAlert',
exit: 'resetFileChange',
on: {
'': [
{ target: 'idle', cond: 'isSvgContextUndefined' },
{ target: 'fileLoaded'},
]
}
},
fileLoaded: {
on: {
click: { target: 'fileLoaded', actions: 'startFileDialog' },
change: { target: 'filePicked', cond: 'isFileListNotEmpty', actions: 'saveFileContext' }
}
},
loadFailed: {
on: {
'': 'idle'
}
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment