Skip to content

Instantly share code, notes, and snippets.

@sesteva
Created December 2, 2019 21:40
Show Gist options
  • Save sesteva/f4d1bc6f446c78e63bdefd0d70afd045 to your computer and use it in GitHub Desktop.
Save sesteva/f4d1bc6f446c78e63bdefd0d70afd045 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const context = {
password: ""
};
const passwordMachine = Machine(
{
initial: "idle",
context,
states: {
idle: {
entry: "clearPassword",
on: {
SUBMIT: { target: "validating", cond: "passwordEntered" },
CHANGE: {
target: "idle",
actions: "assignPassword",
internal: true // this prevents onEntry from running again
}
},
initial: "normal",
states: {
normal: {},
error: {
after: {
2000: "normal"
}
}
}
},
validating: {
onEntry: "validatePassword",
on: {
VALID: "success",
INVALID: "idle.error"
}
},
success: {}
},
on: {
RESET: ".idle"
}
},
{
actions: {
assignPassword: assign({
password: (_, event) => event.value
}),
validatePassword: ctx => {
setTimeout(() => {
if (ctx.password === "password") {
send("VALID");
} else {
send("INVALID");
}
}, 2000);
},
clearPassword: assign({
password: () => {
return "";
}
})
},
guards: {
passwordEntered: ctx => {
return ctx.password && ctx.password.length;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment