Created
July 22, 2019 09:47
-
-
Save mikaelkaron/081d2ea0bf78572b29ad655e9af5df30 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const config = { | |
id: "app", | |
initial: "boot", | |
entry: "params", | |
context: {}, | |
states: { | |
boot: { | |
id: "boot", | |
initial: "pending", | |
states: { | |
pending: { | |
invoke: { | |
src: "boot", | |
onDone: { target: "success", actions: "login" }, | |
onError: "failure" | |
}, | |
after: { TIMEOUT: "failure" } | |
}, | |
success: { type: "final" }, | |
failure: { type: "final" } | |
}, | |
onDone: [{ target: "auth", cond: "isAuth" }, { target: "anon" }] | |
}, | |
anon: { | |
id: "anon", | |
initial: "check", | |
states: { | |
check: { | |
on: { | |
"": [{ target: "signup", cond: "goSignup" }, { target: "signin" }] | |
} | |
}, | |
signup: { | |
entry: "signupEntry", | |
exit: "signupExit", | |
on: { | |
"done.invoke.signup": { target: "done", actions: "login" }, | |
SIGNIN: "signin" | |
}, | |
meta: { component: "page-signup" } | |
}, | |
signin: { | |
entry: "signinEntry", | |
exit: "signinExit", | |
on: { | |
"done.invoke.signin": { target: "done", actions: "login" }, | |
SIGNUP: "signup" | |
}, | |
meta: { component: "page-signin" } | |
}, | |
done: { type: "final" } | |
}, | |
onDone: "#auth", | |
on: { "": { target: "boot", cond: "isAuth" } } | |
}, | |
auth: { | |
id: "auth", | |
initial: "active", | |
states: { | |
signout: { | |
initial: "prompt", | |
states: { | |
prompt: { on: { ABORT: "#hist", LOGOUT: "pending" } }, | |
pending: { | |
invoke: { src: "signout", onDone: "success", onError: "failure" }, | |
after: { TIMEOUT: "failure" } | |
}, | |
success: { type: "final" }, | |
failure: { type: "final" } | |
}, | |
onDone: { target: "#auth.done", actions: "logout" }, | |
meta: { component: "page-signout" } | |
}, | |
active: { | |
initial: "intro", | |
states: { | |
intro: { | |
id: "intro", | |
meta: { component: "page-intro" } | |
}, | |
report: { | |
id: "report", | |
meta: { component: "page-report" } | |
}, | |
hist: { id: "hist", type: "history" } | |
} | |
}, | |
done: { | |
type: "final" | |
} | |
}, | |
onDone: "#anon", | |
on: { "": { target: "#boot", cond: "isAnon" }, SIGNOUT: ".signout" } | |
} | |
}, | |
on: { | |
INTRO: { target: "#intro", internal: true }, | |
REPORT: { target: "#report", internal: true } | |
} | |
}; | |
// DEBUG, should be truggered by sub-machines | |
config.states.anon.states.signup.on["SUBMIT"] = { | |
actions: send({ | |
type: "done.invoke.signup", | |
data: { account: {} } | |
}) | |
}; | |
config.states.anon.states.signin.on["SUBMIT"] = { | |
actions: send({ | |
type: "done.invoke.signin", | |
data: { account: {} } | |
}) | |
}; | |
const options = { | |
actions: { | |
login: assign((ctx, { data }) => ({ ...ctx, ...data })), | |
logout: assign({ | |
account: undefined, | |
profile: undefined | |
}) | |
}, | |
guards: { | |
isAnon: ctx => !ctx.account, | |
isAuth: ctx => !!ctx.account, | |
goSignup: () => false, | |
}, | |
delays: { | |
TIMEOUT: 2000 | |
} | |
}; | |
const machine = Machine(config, options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment