Created
December 2, 2019 21:40
-
-
Save sesteva/f4d1bc6f446c78e63bdefd0d70afd045 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
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