Skip to content

Instantly share code, notes, and snippets.

@bewest
Last active February 5, 2021 17:21
Show Gist options
  • Save bewest/b3bd5535e80bd03b4aa7193f34a875d5 to your computer and use it in GitHub Desktop.
Save bewest/b3bd5535e80bd03b4aa7193f34a875d5 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/*
Our client is expected to access a web service on a regular
interval as part of a polling activity. The access itself
requires a Bearer token, which can sometimes be renewed.
If it cannot be renewed, a new one must be created by
going through a sequence of steps.
*/
var refreshingBearerTokenFetchMachine = Machine({
id: 'RefreshingTokenAccessFetchMachine',
initial: 'idle',
states: {
idle: { on: { FETCH: 'unauthorized' } },
unauthorized: {
initial: 'step_one_portal',
states: {
step_one_portal: {
on: { NEXT: 'step_two_location' }
},
step_two_location: {
on: { NEXT: 'step_three_login' }
},
step_three_login: {
on: { NEXT: 'step_four_consent' }
},
step_four_consent: {
on: { NEXT: 'step_five_bearer' }
},
step_five_bearer: {
on: { NEXT: '#RefreshingTokenAccessFetchMachine.authorized' }
},
}
},
authorized: {
type: 'parallel',
states: {
token: {
initial: 'valid',
states: {
valid: {
on: {
RENEW: 'expiring',
EXPIRE: 'expired'
}
},
expiring: {
on: {
REFRESH: 'refreshing'
}
},
refreshing: {
on: {
RESOLVE: 'valid',
REJECT: 'invalid'
}
},
expired: {
on: {
'': 'invalid'
}
},
invalid: {
on: {
'': '#RefreshingTokenAccessFetchMachine.unauthorized'
}
},
},
},
data: {
initial: 'idle',
states: {
idle: {
on: {
FETCH: [{
target: 'loading',
in: '#RefreshingTokenAccessFetchMachine.authorized.token.valid'
}]
}
},
loading: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
// Does making this a final state mean it can't be re-used in a polling activity?
success: { on: { '': 'idle' } },
failure: {
on: {
RETRY: '#RefreshingTokenAccessFetchMachine.unauthorized'
}
},
}
}
}
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment