Skip to content

Instantly share code, notes, and snippets.

@vunderkind
Last active June 30, 2023 20:02
Show Gist options
  • Save vunderkind/92a0f14497a40f57ed793269780deff2 to your computer and use it in GitHub Desktop.
Save vunderkind/92a0f14497a40f57ed793269780deff2 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
async function invokeFetch(context) {
console.log(context)
const { documentUrl } = context;
return await fetch(`https://google.com`)
}
const DocMachine = Machine({
id: 'doc-chat',
initial: 'idle',
context: {
url: null,
title: null,
},
states: {
idle: {},
selected: {
initial: 'loading',
states: {
loading: {
invoke: {
id: 'get-doc',
src: invokeFetch,
onDone: {
target: 'loaded',
action: assign({
documentUrl: (context, event) => event.url
}),
},
onError: 'error'
},
},
'loaded': {},
'error': {}
}
}
},
on: {
SELECT: {
target: '.selected',
action: assign({
documentUrl: (context, event) => event.url
}),
},
DESELECT: {
target: '.idle',
action: assign({
documentUrl: () => null,
}),
},
},
});
const docSelectEvent = {
type: 'SELECT',
url: 'reactjs'
}
const docDeselectEvent = {
type: 'DESELECT',
url: 'meh'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment