Skip to content

Instantly share code, notes, and snippets.

@tomraithel
Last active Jan 15, 2021
Embed
What would you like to do?
Generated by XState Viz: https://xstate.js.org/viz
const loggedInState = {
initial: 'idle',
states: {
idle: {
on: {
load: 'loading'
}
},
loading: {
invoke: {
id: 'fetchData',
src: 'fetchData',
},
on: {
success: [
{
target: 'idle',
actions: ['updateData']
},
],
error: { target: 'idle', actions: ['setError']
},
},
},
}
};
Machine({
id: 'App State',
initial: 'loadingIdentity',
context: {
identity: null
},
states: {
loadingIdentity: {
invoke: {
id: 'fetchIdentity',
src: 'fetchIdentity',
},
on: {
success: [
{
target: 'loggedIn',
cond: 'hasIdentity',
actions: ['setIdentity']
},
{
target: 'notLoggedIn',
cond: 'hasNoIdentity',
actions: ['clearIdentity']
},
],
error: { target: 'error', actions: ['clearIdentity']
},
},
},
loggedIn: {
...loggedInState
},
notLoggedIn: {
type: 'final'
},
error: {
on: {
FETCH: 'loadingIdentity'
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment