Skip to content

Instantly share code, notes, and snippets.

@hzamir
Last active July 8, 2021 16:27
Show Gist options
  • Save hzamir/37953b89a3bd9694f864cc18f7958c21 to your computer and use it in GitHub Desktop.
Save hzamir/37953b89a3bd9694f864cc18f7958c21 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'Z API 1',
initial: 'idle',
context: {
dog: null,
counter:0,
},
on: {
AA: {
message: 'zounds',
actions: assign((c)=> {
let {counter} = c;
++counter;
return {
...c,
counter
} // end new context
}) // end actions
},
BB: {
target:'idle', internal:true
},
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
invoke: {
id: 'fetchDog',
src: (context, event) => fetch('https://dog.ceo/api/breeds/image/random')
.then(data => data.json()),
onDone: {
target: 'resolved',
actions: assign({
dog: (_, event) => event.data
})
},
onError: 'rejected'
},
on: {
CANCEL: 'idle'
}
},
resolved: {
type: 'final'
},
rejected: {
on: {
FETCH: 'loading'
}
}
}
}, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment