Skip to content

Instantly share code, notes, and snippets.

@jacobparis
Last active March 25, 2020 05:02
Show Gist options
  • Save jacobparis/28dfc6298fc233eb10a5ff8795570ce3 to your computer and use it in GitHub Desktop.
Save jacobparis/28dfc6298fc233eb10a5ff8795570ce3 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 formMachine = Machine({
id: 'form',
initial: 'editing',
context: {
retries: 0,
message: ''
},
states: {
editing: {
on: {
SUBMIT: [
{
target: 'editing.message.error',
cond: 'isMessageTooShort'
},
{
target: 'submitting'
}
],
INPUT: {
target: 'editing',
actions: 'cache'
}
},
type: 'parallel',
states: {
message: {
initial: 'valid',
states: {
valid: {},
error: {}
}
}
}
},
submitting: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'submitting',
actions: 'incrementRetry'
}
}
}
}
}, {
actions: {
cache: assign((context, event) => event),
incrementRetry: assign((context, event) => context.retries + 1)
},
guards: {
isMessageTooShort: (context) => context.message.length < 10
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment