Skip to content

Instantly share code, notes, and snippets.

@40thieves
Last active January 5, 2021 17:54
Show Gist options
  • Save 40thieves/2a4981135827a73fe8e2e19a6649c602 to your computer and use it in GitHub Desktop.
Save 40thieves/2a4981135827a73fe8e2e19a6649c602 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const chatMachine = Machine({
id: "chat",
initial: "idle",
context: {
messages: []
},
states: {
idle: {
on: {
fetch: "fetching",
receive: {
target: "idle",
actions: assign({
messages: (context, event) => [...context.messages, 'new message']
})
}
}
},
fetching: {
invoke: {
src: invokeFetchMessages,
onDone: {
target: "idle",
actions: assign({
messages: (context, event) => [...event.data, ...context.messages]
})
}
}
}
}
});
function invokeFetchMessages() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(['foo', 'bar'])
}, 5000)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment