Skip to content

Instantly share code, notes, and snippets.

@arcdev1
Last active November 26, 2019 19:17
Show Gist options
  • Save arcdev1/d5fb4f0adec00e1c4887ff938585574f to your computer and use it in GitHub Desktop.
Save arcdev1/d5fb4f0adec00e1c4887ff938585574f 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 loginMachine = Machine({
id: 'login',
initial: 'ready',
context: {
email: null,
password: null,
rememberMe: false
},
states: {
ready: {},
waiting: {},
loggedIn: {
type: 'final',
data: {
session: (_, e) => e.session
}
},
error: {}
}
})
const appMachine = Machine({
id: 'app',
initial: 'anonymous',
context: {
session: null
},
states: {
anonymous: {
on: {
CLICK_LOGIN: 'loggingIn',
CLICK_JOIN: 'signingUp',
AUTHENTICATED: {
target: 'loggedIn',
actions: 'cacheSession'
}
}
},
loggingIn: {
invoke: {
id: 'login-machine',
src: loginMachine,
onDone: {
target: 'loggedIn',
actions: 'cacheSession'
}
}
},
loggedIn: {},
signingUp: {
invoke: {
id: 'signup-machine',
src: 'signupMacine',
onDone: {
target: 'loggedIn',
actions: 'cacheSession'
}
}
}
},
actions: {
cacheSession:
assign({session: (_ , e) => e.session || e.data.session})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment