Skip to content

Instantly share code, notes, and snippets.

@miraklez
Last active October 28, 2020 08:55
Show Gist options
  • Select an option

  • Save miraklez/9f92ff772e603ef56c924e74bf978d74 to your computer and use it in GitHub Desktop.

Select an option

Save miraklez/9f92ff772e603ef56c924e74bf978d74 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 BrowserWebSocketMachine = Machine({
id: 'browser-websocket',
initial: 'idle',
context: {
url: null,
protocols: null,
socket: null
},
states: {
idle: {
on: {
OPEN: {
target: 'connecting',
cond: 'isValidURL',
actions: 'setParams'
}
}
},
connecting: {
entry: {
actions: 'initSocket'
}
},
connected: {},
failure: {}
}
}, {
guards: {
isValidURL: (_, { url }) => /^ws[s]?:\/\/.+/.test(url)
},
actions: {
setParams: (ctx, { url, protocols }) => {
ctx.url = url
ctx.protocols = protocols
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment