Skip to content

Instantly share code, notes, and snippets.

@conorhastings
Last active April 9, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conorhastings/1d2587cef368bab0ad409e5124314db4 to your computer and use it in GitHub Desktop.
Save conorhastings/1d2587cef368bab0ad409e5124314db4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const PIN = 1234;
const pinMachine = Machine({
id: "wizard",
context: {
phone: "",
pin: "",
retries: 0
},
initial: "phone",
states: {
phone: {
on: {
NEXT: {
target: "pin",
actions: assign({ phone: (_, e) => e.value })
}
}
},
pin: {
on: {
NEXT: {
target: "checkPin",
actions: assign({ pin: (_, e) => e.value }),
}
}
},
checkPin: {
invoke: {
id: "checkPin",
src: (context) => context.pin === PIN,
actions: assign({ pin: (context) => context.retires++ }),
cond: (context) => context.retries < 3,
onDone: "success",
onError: "failure"
}
},
failure: {
on: {
RETRY: "checkPin",
}
},
success: {
type: "final"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment