Skip to content

Instantly share code, notes, and snippets.

@sylvanaar
Last active August 13, 2020 19:25
Show Gist options
  • Save sylvanaar/89405267cca62fe48369310d811e52fd to your computer and use it in GitHub Desktop.
Save sylvanaar/89405267cca62fe48369310d811e52fd 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 authMachine = Machine(
{
id: "auth",
initial: "loggedout",
context: {
retries: 0,
timeout: 5000,
},
states: {
loggedout: {
entry: (context, event) => console.log("loggedout entry", context),
on: {
AUTHENTICATE: "authenticating",
},
},
authenticating: {
entry: (context, event) => console.log("authenticating entry", context),
invoke: [{ src: "TIMER" }, { src: "SEND_AUTH" }],
on: {
AUTH_SUCCESS: "loggedin",
AUTH_FAIL: "loggedout",
TIME_OUT: "loggedout",
},
},
loggedin: {
type: "final",
},
},
},
{
actions: {
// action implementations
AUTHENTICATE: (context, event) => {
console.log("calling auth...");
},
AUTH_SUCCESS: (context, event) => {
console.log("recieved success response");
},
AUTH_FAIL: (context, event) => {
console.log("receoved fail response");
},
},
services: {
SEND_AUTH: (context, event) => {
console.log("sendging auth", context, event);
},
TIMER: (context, activity) => (callback, onReceive) => {
console.log("timer started");
const id = setTimeout(() => {
console.log("timeout");
callback({ type: "TIME_OUT" });
}, context.timeout);
return () => {
console.log("clear timer");
clearTimeout(id);
};
},
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment