Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Created June 25, 2021 17:59
Show Gist options
  • Save austinsamsel/cb1003cfc4fde66c501d11bad6424b4e to your computer and use it in GitHub Desktop.
Save austinsamsel/cb1003cfc4fde66c501d11bad6424b4e 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 MAX_POLL_DURATION = 1000 * 60;
const FS = {
IDLE: 'IDLE',
LOADING: 'LOADING',
AUTH: 'AUTH',
NO_AUTH: 'NO_AUTH',
ERROR: 'ERROR',
AUTHENTICATING: 'AUTHENTICATING',
};
const EVENTS = {
SIGN_IN_SESSION: 'SIGN_IN_SESSION',
CLEAR_SESSION: 'CLEAR_SESSION',
SIGN_UP_WALLET: 'SIGN_UP_WALLET',
SIGN_IN_WALLET: 'SIGN_IN_WALLET',
CLEAR_WALLET: 'CLEAR_WALLET',
}
const definition = {
id: 'session',
type: 'parallel',
context: {
FS,
error: undefined,
},
states: {
oauth: {
initial: FS.LOADING,
states: {
[FS.LOADING]: {
invoke: {
src: 'getSession',
onDone: [
{
target: FS.AUTH,
actions: 'assignAccount',
cond: 'isAuthenticated',
},
{
target: FS.NO_AUTH,
},
],
onError: {
target: FS.ERROR,
actions: 'assignError',
},
},
},
[FS.AUTHENTICATING]: {
invoke: {
src: 'signIn',
onDone: [
{
target: FS.AUTH,
actions: 'assignAccount',
},
],
onError: {
target: FS.ERROR,
actions: 'assignError',
},
},
},
[FS.AUTH]: {},
[FS.NO_AUTH]: {},
[FS.ERROR]: {},
},
on: {
[EVENTS.SIGN_IN_SESSION]: {
target: `oauth.${FS.LOADING}`,
},
[EVENTS.CLEAR_SESSION]: {
target: `oauth.${FS.LOADING}`,
},
},
},
fcl: {
initial: FS.LOADING,
states: {
[FS.LOADING]: {
invoke: {
src: 'fclCurrentUser',
onDone: [
{
target: FS.AUTH,
actions: 'assignFclAccount',
cond: 'isAuthenticated',
},
{
target: FS.NO_AUTH,
},
],
onError: {
target: FS.ERROR,
actions: 'assignError',
},
},
},
[FS.AUTHENTICATING]: {
invoke: {
src: 'fclSignUp', // 'fclSignIn'
onDone: [
{
target: FS.AUTH,
actions: 'assignFclAccount',
},
],
onError: {
target: FS.ERROR,
actions: 'assignError',
},
},
},
[FS.AUTH]: {},
[FS.NO_AUTH]: {},
[FS.ERROR]: {},
},
on: {
[EVENTS.SIGN_UP_WALLET]: {
target: `fcl.${FS.AUTHENTICATING}`,
},
[EVENTS.SIGN_IN_WALLET]: {
target: `fcl.${FS.AUTHENTICATING}`,
},
[EVENTS.CLEAR_WALLET]: {
target: `fcl.${FS.NO_AUTH}`,
},
},
},
},
};
const services = {
fclSend: () => {},
fclOnceSealed: () => {},
};
const guards = {
isAuthorized: (ctx) => true,
};
const machine = Machine(definition,
{
actions,
services,
guards,
delays: { MAX_POLL_DURATION },
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment