Last active
October 17, 2020 19:36
-
-
Save karfau/7711b52a5cb2e43e59d24b00e3c60b53 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz/?gist=7711b52a5cb2e43e59d24b00e3c60b53
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 CHANGE = [ | |
{ | |
actions: ['log', 'storeCurrent'] | |
}, | |
{ | |
cond: 'isChanged', | |
target: 'validating' | |
}, | |
{target: 'unchanged'} | |
] | |
const fieldMachine = Machine({ | |
id: 'fetch', | |
initial: 'unchanged', | |
context: { | |
original: 'one', | |
current: 'one' | |
}, | |
states: { | |
unchanged: { | |
on: {CHANGE} | |
}, | |
validating: { | |
on: { | |
CHANGE, | |
VALID: 'submittable', | |
INVALID: 'invalid' | |
} | |
}, | |
invalid: { | |
on: {CHANGE} | |
}, | |
submittable: { | |
CHANGE, | |
SUBMIT: { | |
actions: 'save', | |
target: 'unchanged' | |
} | |
}, | |
} | |
},{ | |
actions:{ | |
log: console.error, | |
storeCurrent: assign({current: (_, event) => event.value || '' | |
}), | |
save: assign({original: (c) => c.current}) | |
}, | |
guards:{ | |
isChanged: c => { | |
console.log('isChanged', c) | |
return c.current !== c.original | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This machine has several issues, the one I got stuck with (including many possible solutions):
statelyai/xstate#1565
The solution I picked and other fixes are in a second gist:
https://gist.github.com/karfau/760d85cb7ed38017a31f958fef883c7e