Last active
October 23, 2020 09:35
-
-
Save miraklez/b143a1f925a38ad79214dc8317ec89f2 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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