Skip to content

Instantly share code, notes, and snippets.

@iamstarkov
Last active February 14, 2020 12:24
Show Gist options
  • Save iamstarkov/8f52126481d9bcdc799549dcf943fb54 to your computer and use it in GitHub Desktop.
Save iamstarkov/8f52126481d9bcdc799549dcf943fb54 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine(
{
id: 'OAUTH CLIENT',
type: 'parallel',
context: {
hasAuthorizationCode: true
},
states: {
AUTHORIZED: {
id: 'authed',
initial: 'FALSE',
states: {
TRUE: {
// on:
},
FALSE: {}
}
},
CALLBACK: {
initial: 'INIT',
id: 'callback',
states: {
'INIT': {
on: {
'*': [
{
target: 'REQUESTING ACCESS TOKEN',
cond: 'hasAuthorizationCode'
},
{
target: '#authed.FALSE'
}
]
}
},
'REQUESTING ACCESS TOKEN': {
id: 'access_token',
entry: 'exchangeAuthCodeForAccessToken',
initial: 'idle',
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
type: 'final',
onEntry: send('SUCC'),
},
failure: {
type: 'final',
onEntry: send('FAIL'),
}
},
on: {
SUCC: 'REQUESTING SESSION',
FAIL: '#authed.FALSE'
}
},
'REQUESTING SESSION': {
entry: 'exchangeAccessTokenForSession',
on: {
SUCC: {
target: [
'#authed.TRUE',
'SESSION RECEIVED'
]
},
FAIL: '#authed.FALSE'
}
},
'SESSION RECEIVED': {
type: 'final',
}
}
}
}
},
{ guards: { hasAuthorizationCode: ctx => ctx.hasAuthorizationCode } }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment