Skip to content

Instantly share code, notes, and snippets.

@the-main-thing
Last active June 6, 2020 14:32
Show Gist options
  • Save the-main-thing/952b2cd98f97f5e254a84516fd70bb32 to your computer and use it in GitHub Desktop.
Save the-main-thing/952b2cd98f97f5e254a84516fd70bb32 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine( {
id: "connection",
initial: "disconnected",
context: {
wsServerUrl: null,
socket: null,
error: null,
recievedPacket: null,
sentPacket: null,
},
states: {
disconnected: {
initial: "idle",
on: {
ERROR: {
actions: ["setError"],
},
REMOVE_ERROR: {
actions: ["removeError"],
},
},
states: {
idle: {
on: {
DOWNLOAD_PACKET: {
target: "downloading",
},
CONNECT: {
target: "#connection.connecting",
},
},
},
downloading: {
invoke: {
src: "downloadPacket",
onDone: {
target: "idle",
actions: ["setSentPacket", "removeError"],
},
onError: {
target: "idle",
actions: ["setError"],
},
},
},
},
},
connected: {
initial: "idle",
on: {
ERROR: {
actions: ["setError"],
},
REMOVE_ERROR: {
actions: ["removeError"],
},
},
states: {
idle: {
on: {
DOWNLOAD_PACKET: {
target: "downloading",
},
SEND_PACKET: [
{
cond: {
type: "packetDownloaded",
},
target: "sending",
},
],
RECIEVE_PACKET: {
target: "recieving",
},
DISCONNECT: {
target: "#connection.disconnected",
actions: ["disconnect", "removeSocket"],
},
},
},
downloading: {
invoke: {
src: "downloadPacket",
onDone: {
target: "idle",
actions: ["setSentPacket", "removeError"],
},
onError: {
target: "idle",
actions: ["setError"],
},
},
},
sending: {
invoke: {
src: "sendPacket",
onDone: {
target: "idle",
actions: ["markPacketAsSent", "removeError"],
},
onError: {
target: "idle",
actions: ["setError"],
},
},
},
recieving: {
invoke: {
src: "parseIncomingPacket",
onDone: {
target: "idle",
actions: ["setRecievedPacket", "removeError"],
},
onError: {
target: "idle",
actions: ["setError"],
},
},
},
},
},
connecting: {
invoke: {
src: "connectToWsServer",
onDone: {
target: "connected",
actions: ["setSocket", "removeError"],
},
onError: {
target: "disconnected",
actions: ["setError"],
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment