Skip to content

Instantly share code, notes, and snippets.

@LevelbossMike
Created November 16, 2020 12:27
Show Gist options
  • Select an option

  • Save LevelbossMike/575376b37db8132f1af120b449dd3a8d to your computer and use it in GitHub Desktop.

Select an option

Save LevelbossMike/575376b37db8132f1af120b449dd3a8d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
function establishConnection() {}
function sendHeartbeat() {}
const fetchMachine = Machine({
initial: 'disconnected',
states: {
disconnected: {
on: {
CONNECT: 'connecting'
}
},
connecting: {
invoke: {
id: 'establishingConnection',
src: establishConnection,
onDone: 'connected',
onError: 'error'
}
},
connected: {
initial: 'idle',
states: {
idle: {
after: [
{
delay: 5000,
target: 'beating'
}
]
},
beating: {
invoke: {
src: 'sendHeartbeat',
onDone: 'idle',
onError: '#error'
}
}
}
},
error: {
id: 'error',
on: {
'': [
{
target: 'connecting',
cond: 'shouldRetryConnection',
actions: [
assign({
connectionRetries: ({ connectionRetries }) => connectionRetries + 1
})
]
},
{
actions: ['showReconnectDialog']
}
],
RECONNECT: {
target: 'connecting',
actions: [
assign({
connectionRetries: 0
})
]
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment