Last active
March 25, 2020 05:01
-
-
Save jacobparis/365c0512bcdd1d187a8fda64f4211daa to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 formMachine = Machine({ | |
id: 'form', | |
initial: 'editing', | |
context: { | |
retries: 0, | |
message: '' | |
}, | |
states: { | |
editing: { | |
on: { | |
SUBMIT: [ | |
{ | |
target: 'editing', | |
cond: 'isMessageTooShort' | |
}, | |
{ | |
target: 'submitting' | |
} | |
], | |
INPUT: { | |
target: 'editing', | |
actions: 'cache' | |
} | |
} | |
}, | |
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