Skip to content

Instantly share code, notes, and snippets.

@Jayphen
Last active January 6, 2020 16:08
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 Jayphen/858478ab6ad0ab56b7ddd3bf312f15a8 to your computer and use it in GitHub Desktop.
Save Jayphen/858478ab6ad0ab56b7ddd3bf312f15a8 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 fetchCuteAnimals = () => {
return fetch('https://www.reddit.com/r/aww.json')
.then(res => res.json())
.then(res => res.data.children.map(child => child.data.title))
}
const cuteAnimalMachine = Machine({
id: 'cuteAnimals',
initial: 'idle',
context: {
cuteAnimals: null,
error: null,
},
states: {
idle: {
on: { FETCH: 'loading' },
},
loading: {
invoke: {
id: 'fetchCuteAnimals',
src: fetchCuteAnimals,
onDone: {
target: 'success',
actions: [
assign({
cuteAnimals: (context, event) => event.data,
}),
],
},
onError: {
target: 'failure',
actions: [
assign({
error: (context, event) => event.data,
}),
],
},
},
},
success: {
type: 'final',
},
failure: {
on: {
RETRY: 'loading',
},
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment