Skip to content

Instantly share code, notes, and snippets.

@pke
Created March 11, 2021 14:57
Show Gist options
  • Save pke/cdda1f720bfeafa1ed4c358e217185eb to your computer and use it in GitHub Desktop.
Save pke/cdda1f720bfeafa1ed4c358e217185eb 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)
// - Rumanus
const fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
password: "",
passwordConfirmation: "",
savedAsImage: false,
savedAsFile: false,
sendImageAsEmail: false,
sendFileAsEmail: false,
savedNumberOfTimes: 0
},
states: {
idle: {
on: {
EXPORT: {
target: 'export',
},
IMPORT: "import"
}
},
"export": {
initial: "password",
states: {
"password": {
on: {
CHANGE_PASSWORD: {
actions: assign({
password: (context, event) =>
event.value
}),
target: "password"
},
CHANGE_CONFIRMATION: {
actions: assign({
passwordConfirmation: (context, event) =>
event.value
}),
target: "password"
},
"DONE": {
cond: (context) =>
context.password === context.passwordConfirmation,
target: "save"
},
}
},
"save": {
on: {
"SAVED": {
cond: (context) => context.savedNumberOfTimes >= 2,
target: "confirm"
},
"saveAsImage": {
actions: assign({
savedAsImage: (context, event) =>
true,
savedNumberOfTimes: (context) => ++context.savedNumberOfTimes
})
},
"saveAsFile": {
actions: assign({
savedAsFile: (context, event) =>
true,
savedNumberOfTimes: (context) => ++context.savedNumberOfTimes
})
},
"sendImageAsEmail": {
actions: assign({
sendImageAsEmail: (context, event) =>
true,
savedNumberOfTimes: (context) => ++context.savedNumberOfTimes
})
},
"sendFileAsEmail": {
actions: assign({
sendFileAsEmail: (context, event) =>
true,
savedNumberOfTimes: (context) => ++context.savedNumberOfTimes
})
}
}
},
"confirm": {
on: {
cancel: "save",
ok: "done"
}
},
done: {
type: "final"
}
}
},
"import": {
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment