Skip to content

Instantly share code, notes, and snippets.

@believer
Created March 23, 2020 19:28
Show Gist options
  • Save believer/522cd49443cff514be919c191de198c4 to your computer and use it in GitHub Desktop.
Save believer/522cd49443cff514be919c191de198c4 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 fetchMachine = Machine({
id: 'toggle',
initial: 'empty',
context: {
message: '',
messages: [],
},
states: {
empty: {},
hasText: {
on: {
COMMIT: {
target: 'commit',
cond: ctx => ctx.message.trim().length > 0,
},
RESET: {
target: 'empty',
actions: assign({ message: '' }),
},
},
},
commit: {
invoke: {
src: (ctx, ev) => Promise.resolve(ev.value),
onDone: {
target: 'empty',
actions: assign({
message: '',
messages: (ctx, ev) => [...ctx.messages, ev.data],
}),
},
},
},
},
on: {
TYPING: {
target: 'hasText',
actions: assign({
message: (ctx, ev) => ev.value,
}),
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment