Skip to content

Instantly share code, notes, and snippets.

@40thieves
Last active January 6, 2021 09:35
Show Gist options
  • Save 40thieves/23aa888f7527721be5d138d8446124a7 to your computer and use it in GitHub Desktop.
Save 40thieves/23aa888f7527721be5d138d8446124a7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const chatMachine = Machine({
id: "chat",
type: "parallel",
context: {
messages: []
},
states: {
http: {
initial: "idle",
states: {
idle: {
on: {
fetch: "fetching"
}
},
fetching: {
invoke: {
src: invokeFetchMessages,
onDone: {
target: "idle",
actions: assign({
messages: (context, event) => {
return [...event.data, ...context.messages];
}
})
}
}
}
}
},
socket: {
initial: "idle",
states: {
idle: {
on: {
receive: {
target: "idle",
actions: assign({
messages: (context, event) => [
...context.messages,
"new message"
]
})
}
}
}
}
}
}
});
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