Skip to content

Instantly share code, notes, and snippets.

@willmcclellan
Last active June 3, 2021 02:02
Show Gist options
  • Save willmcclellan/ce676fc81163db0d5e7a809608beb926 to your computer and use it in GitHub Desktop.
Save willmcclellan/ce676fc81163db0d5e7a809608beb926 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const authMachine = Machine({
id: 'auth',
initial: 'unauthenticated',
context: {
tenantId: null,
sharedUser: null,
},
states: {
unauthenticated: {
on: {
AUTHENTICATE_INSECURE: 'authenticatingInsecure',
AUTHENTICATE: 'authenticating',
}
},
authenticating: {
invoke: {
id: 'authenticate',
src: (context, event) => fetchUser(context.userId),
onDone: {
target: 'authenticated',
// actions: assign({ user: (context, event) => event.data })
},
onError: {
target: 'unauthenticated',
// actions: assign({ error: (context, event) => event.data })
}
}
},
authenticated: {
initial: 'singleUserMode',
on: {
UNAUTHENTICATE: 'unauthenticated',
},
states: {
singleUserMode: {
on: {
SET_SHARED_MODE: 'sharedUserMode',
SET_APPLICATION: {
actions: 'setApplicationId',
}
}
},
sharedUserMode: {
on: {
ATTACH_USER: 'attachingUser',
UNATTACH_USER: {
target: 'singleUserMode',
actions: 'unsetSharedUser',
},
BOOK_ON: {
target: 'bookingOn',
// TODO another condition, can't book on if already booked on
cond: context => context.sharedUser,
actions: ''
},
// TODO book off
}
},
attachingUser: {
invoke: {
id: 'attachUser',
src: (context, event) => fetchSharedUser(context.userId),
onDone: {
target: 'sharedUserMode',
actions: 'setSharedUser',
},
onError: {
target: 'sharedUserMode',
// actions: assign({ error: (context, event) => event.data })
}
}
},
bookingOn: {
invoke: {
id: 'bookOn',
onDone: {
target: 'sharedUserMode',
actions: 'setBookedOn',
},
// set booked on context
}
}
}
},
authenticatingInsecure: {
invoke: {
id: 'authenticateInsecure',
src: (context, event) => fetchInsecureUser(context.userId),
onDone: {
target: 'authenticatedInsecure',
// actions: assign({ user: (context, event) => event.data })
},
onError: {
target: 'unauthenticated',
// actions: assign({ error: (context, event) => event.data })
}
}
},
authenticatedInsecure: {
on: {
BOOK_ON: {
target: 'bookingOn',
actions: ''
},
UNAUTHENTICATE: 'unauthenticated',
},
},
bookingOn: {
invoke: {
id: 'bookOn',
onDone: {
target: 'authenticatedInsecure',
actions: 'setBookedOn',
},
// set booked on context
}
}
}
}, {
actions: {
setApplicationId: assign({
applicationId: (context, event) => 'app1'
}),
unsetTenantId: assign({
tenantId: (context, event) => null
}),
setSharedUser: assign({
sharedUser: (context, event) => 'rob'
}),
unsetSharedUser: assign({
sharedUser: (context, event) => null
}),
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment