Skip to content

Instantly share code, notes, and snippets.

@cahnory
Last active November 5, 2020 21:33
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 cahnory/92dbf12508e2991e0d34d8385412c851 to your computer and use it in GitHub Desktop.
Save cahnory/92dbf12508e2991e0d34d8385412c851 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 fetchMachine = Machine({
id: 'File',
initial: 'unknown',
context: {
path: '',
encoding: 'utf-8',
content: '',
parse: JSON.parse,
},
states: {
unknown: {
on: {
READ: 'reading',
},
},
notFound: {
on: {
READ: 'reading',
},
},
malformed: {
on: {
READ: 'reading',
},
},
resolved: {
on: {
READ: 'reading',
},
},
reading: {
invoke: {
id: 'readJSON',
src: async (ctx) => '{"foo":"bar"}',
onDone: {
target: 'parsing',
actions: assign({
content: (_, { data }) => data,
}),
},
onError: {
target: 'notFound',
actions: assign({
content: '',
}),
},
},
},
parsing: {
invoke: {
id: 'parseJSON',
src: async ({ content, parse }) => parse(content),
onDone: {
target: 'resolved',
actions: assign({
content: (_, event) => event.data,
}),
},
onError: {
target: 'malformed',
actions: assign({
content: '',
}),
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment