Skip to content

Instantly share code, notes, and snippets.

@wesleycoder
Last active July 27, 2020 16:24
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 wesleycoder/9938615697c01d4ef0d0bebb6cea6642 to your computer and use it in GitHub Desktop.
Save wesleycoder/9938615697c01d4ef0d0bebb6cea6642 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const appMachine = Machine({
id: "App",
initial: "CLOSED",
context: {
isAuthenticated: false,
},
states: {
CLOSED: {
id: "closed",
on: {
open: "STARTING",
},
},
STARTING: {
id: "starting",
invoke: {
id: "startApp",
src: 'startApp',
onDone: "#started",
onError: "#crashed",
},
},
STARTED: {
id: "started",
on: {
"": [
{
target: "AUTHENTICATED",
cond: (ctx) => ctx.isAuthenticated,
},
{ target: "NOT_AUTHENTICATED", cond: (ctx) => !ctx.isAuthenticated },
],
},
},
AUTHENTICATED: {
id: "authenticated",
initial: 'HOME',
states: {
HOME: {
on: {
goToProfile: 'PROFILE'
},
},
PROFILE: {
on: {
get logout () {
return appMachine.states.NOT_AUTHENTICATED
},
},
},
},
},
NOT_AUTHENTICATED: {
id: "notAuthenticated",
initial: "LOGIN",
invoke: (ctx) => ctx.isAuthenticated = false,
states: {
LOGIN: {
on: {
get login () {
return {
target: appMachine.states.AUTHENTICATED,
actions: (ctx) => {
ctx.isAuthenticated = true;
}
}
},
},
},
SIGN_UP: {}
},
},
CRASHED: {
id: "crashed",
type: "final",
},
},
}, {
services: {
startApp: async () => new Promise(
(res, rej) =>
setTimeout(res, 1000)
)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment