Skip to content

Instantly share code, notes, and snippets.

@miraklez
Last active October 23, 2020 09:35
Show Gist options
  • Save miraklez/b143a1f925a38ad79214dc8317ec89f2 to your computer and use it in GitHub Desktop.
Save miraklez/b143a1f925a38ad79214dc8317ec89f2 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)
const incrementRetries = assign({
retries: (ctx) => ctx.retries +1
})
const resetRetries = assign({ retries: 0 })
const isAutoConnect = (ctx) => ctx.autoConnect
const ppStates = {
id: 'ping-pong',
initial: 'waitingresp',
states: {
on: {
PONG: 'waitingresp'
}
}
}
const websocket = Machine({
id: 'websocket',
initial: 'idle',
context: {
retries: 0,
autoConnect: true
},
states: {
idle: {
on: {
OPEN: 'connecting'
}
},
connecting: {
on: {
SUCCESS: 'connected',
ERROR: 'failure'
}
},
connected: {
entry: resetRetries,
...ppStates
},
failure: {
on: {
RETRY: {
target: 'connecting',
actions: incrementRetries,
}
},
after: {
RETRY_DELAY: {
target: 'connecting',
cond: isAutoConnect,
actions: incrementRetries
}
}
}
}
},{
delays: {
RETRY_DELAY: (ctx, ev) => 500
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment