Last active
September 29, 2021 21:57
-
-
Save austinsamsel/0a2b3e3fb82be306b6df359270494283 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const FS = { | |
IDLE: 'IDLE', | |
LOADING: 'LOADING', | |
AUTHENTICATED: 'AUTHENTICATED', | |
NOT_AUTHED: 'NOT_AUTHED', | |
AUTH_PROCESSING: 'AUTH_PROCESSING', | |
REDIRECT: 'REDIRECT', | |
ERROR: 'ERROR', | |
PATCHING_PROFILE: 'PATCHING_PROFILE', | |
}; | |
const EVENTS = { | |
SIGN_IN: 'SIGN_IN', | |
SIGN_OUT: 'SIGN_OUT', | |
UPDATE: 'UPDATE', | |
SIGN_UP_WALLET: 'SIGN_UP_WALLET', | |
SIGN_IN_WALLET: 'SIGN_IN_WALLET', | |
SIGN_OUT_WALLET: 'SIGN_OUT_WALLET', | |
} | |
const ERRORS = { | |
UNKNOWN_WALLET_EVENT: 'UNKNOWN_WALLET_EVENT', | |
UNKNOWN_AUTH_EVENT: 'UNKNOWN_AUTH_EVENT', | |
SESSION_ERROR: 'SESSION_ERROR', | |
PATCH_AUTH_INFO: 'PATCH_AUTH_INFO', | |
ALTERNATE_WALLET: 'ALTERNATE_WALLET', | |
PATCH_WALLET_INFO: 'PATCH_WALLET_INFO', | |
} | |
const definition = { | |
id: 'session', | |
type: 'parallel', | |
context: { | |
FS, | |
EVENTS, | |
ERRORS, | |
auth: undefined, | |
wallet: undefined, | |
profile: undefined, | |
client: undefined, | |
errorWallet: undefined, | |
errorAuth: undefined, | |
}, | |
states: { | |
auth: { | |
initial: FS.LOADING, | |
states: { | |
[FS.LOADING]: { | |
invoke: { | |
src: 'getSession', | |
onDone: [ | |
{ | |
target: FS.AUTHENTICATED, | |
actions: 'assignAuth', | |
cond: 'isAuthenticated', | |
}, | |
{ | |
target: FS.NOT_AUTHED, | |
}, | |
], | |
onError: { | |
target: FS.NOT_AUTHED, | |
}, | |
}, | |
}, | |
[FS.REDIRECT]: { | |
invoke: { | |
src: 'authRedirect', | |
}, | |
}, | |
[FS.AUTHENTICATED]: { | |
initial: FS.PATCHING_PROFILE, | |
states: { | |
[FS.PATCHING_PROFILE]: { | |
invoke: { | |
src: 'patchAuthInfo', | |
onDone: [ | |
{ | |
target: FS.IDLE, | |
actions: 'assignProfile', | |
}, | |
], | |
onError: { | |
target: '#authError', | |
actions: 'assignErrorAuth', | |
}, | |
}, | |
}, | |
[FS.IDLE]: {}, | |
}, | |
}, | |
[FS.NOT_AUTHED]: {}, | |
[FS.ERROR]: { | |
id: 'authError', | |
}, | |
}, | |
on: { | |
[EVENTS.SIGN_IN]: { | |
target: `auth.${FS.REDIRECT}`, | |
}, | |
[EVENTS.SIGN_OUT]: { | |
target: `auth.${FS.REDIRECT}`, | |
}, | |
}, | |
}, | |
wallet: { | |
initial: FS.LOADING, | |
states: { | |
[FS.LOADING]: {}, | |
[FS.AUTH_PROCESSING]: { | |
invoke: { | |
src: 'fclAuth', | |
}, | |
}, | |
[FS.AUTHENTICATED]: { | |
initial: FS.PATCHING_PROFILE, | |
entry: 'assignWallet', | |
states: { | |
[FS.PATCHING_PROFILE]: { | |
invoke: { | |
src: 'patchWalletInfo', | |
onDone: [ | |
{ | |
target: FS.IDLE, | |
actions: 'assignProfile', | |
}, | |
], | |
onError: { | |
target: '#walletError', | |
actions: 'assignErrorWallet', | |
}, | |
}, | |
}, | |
[FS.IDLE]: {}, | |
}, | |
}, | |
[FS.NOT_AUTHED]: { | |
entry: 'clearWallet', | |
}, | |
[FS.ERROR]: { | |
id: 'walletError', | |
}, | |
}, | |
on: { | |
[EVENTS.UPDATE]: [ | |
{ | |
target: `wallet.${FS.AUTHENTICATED}`, | |
cond: 'hasWallet', | |
}, | |
{ | |
target: `wallet.${FS.NOT_AUTHED}`, | |
}, | |
], | |
[EVENTS.SIGN_UP_WALLET]: { | |
target: `wallet.${FS.AUTH_PROCESSING}`, | |
}, | |
[EVENTS.SIGN_IN_WALLET]: { | |
target: `wallet.${FS.AUTH_PROCESSING}`, | |
}, | |
[EVENTS.SIGN_OUT_WALLET]: { | |
target: `wallet.${FS.AUTH_PROCESSING}`, | |
}, | |
}, | |
}, | |
}, | |
}; | |
const services = {}; | |
const guards = { | |
isAuthenticated: () => true, | |
hasWallet: () => true, | |
}; | |
const machine = Machine(definition, | |
{ | |
actions, | |
services, | |
guards, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment