Skip to content

Instantly share code, notes, and snippets.

@tigranpetrossian
Created July 18, 2021 12:19
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 tigranpetrossian/b3479a24ef3e1bee65725eda2b9ae6e6 to your computer and use it in GitHub Desktop.
Save tigranpetrossian/b3479a24ef3e1bee65725eda2b9ae6e6 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 fetchMachine = Machine({
id: 'session',
initial: 'checkingAuth',
context: {
loginError: null,
user: null,
},
states: {
checkingAuth: {
on: {
REPORT_TOKEN_EXISTS: { target: 'authenticated' },
REPORT_TOKEN_DOESNT_EXIST: { target: 'unauthenticated' },
},
invoke: {
id: 'authCheck',
src: 'checkAuthentication',
},
},
unauthenticated: {
initial: 'idle',
states: {
idle: {},
error: {},
},
on: {
LOG_IN: {
target: 'loggingIn',
},
},
},
loggingIn: {
invoke: {
id: 'login',
src: 'login',
onDone: {
target: 'authenticated',
actions: ['removeLoginError', 'persistToken'],
},
onError: {
target: 'unauthenticated.error',
actions: ['setError'],
},
},
},
authenticated: {
initial: 'initializingApp',
on: {
LOG_OUT: {
target: 'unauthenticated',
actions: ['removeToken'],
},
},
invoke: {
id: 'initialize-app',
src: 'initializeApp',
},
states: {
initializingApp: {
on: {
SET_ACTIVE: 'active',
},
},
active: {
on: {
SWITCH_TENANT: { target: 'switchingTenant' },
},
},
switchingTenant: {
on: {
SWITCH_TENANT_SUCCESS: { target: 'active' },
},
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment