Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active November 10, 2020 22:47
Show Gist options
  • Save austinsamsel/b0aa5f280e21d62957dd33268d6ec74f to your computer and use it in GitHub Desktop.
Save austinsamsel/b0aa5f280e21d62957dd33268d6ec74f 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 FS = {
IDLE: 'idle',
VALIDATING: 'validating',
PURCHASING: 'purchasing',
POLLING_ORDER: 'pollingOrder',
REDIRECT_TO_BUY: 'redirectToBuy',
RESET: 'reset',
DONE: 'done'
};
const IGNORE_ERRORS_DURATION = 60 * 1000;
Machine(
{
id: 'purchasePack',
initial: 'idle',
states: {
[FS.IDLE]: {
on: {
PURCHASE: [
{
target: '#purchasePack.validating',
cond: 'isRecaptchaEnabled',
},
{
target: '#purchasePack.purchasing',
actions: 'setMessage',
},
],
},
},
[FS.VALIDATING]: {
invoke: {
id: 'validating',
src: 'executeRecaptcha',
onDone: {
target: '#purchasePack.purchasing',
actions: 'setMessage',
},
onError: {
target: '#purchasePack.reset',
actions: 'setErrorMessage',
},
},
},
[FS.PURCHASING]: {
invoke: {
id: 'purchasing',
src: 'purchasePack',
onDone: {
target: '#purchasePack.pollingOrder',
actions: 'setMessage',
},
onError: {
target: '#purchasePack.reset',
actions: 'setErrorMessage',
},
},
},
[FS.POLLING_ORDER]: {
invoke: {
id: 'pollingOrder',
src: 'pollOrder',
onError: {
target: '#purchasePack.reset',
actions: 'setErrorMessage',
},
},
on: {
POLL_ORDER_ERROR: {
target: '#purchasePack.reset',
actions: 'setErrorMessage',
},
POLL_ORDER_SUCCESS: {
target: '#purchasePack.redirectToBuy',
actions: 'setMessage',
},
},
after: {
[IGNORE_ERRORS_DURATION]: {
actions: send('STOP', { to: 'pollingOrder' }),
},
},
},
[FS.REDIRECT_TO_BUY]: {
entry: 'setDoneMessage',
invoke: {
id: 'redirectToBuy',
src: 'redirectToBuy',
type: 'final',
},
},
[FS.RESET]: {
on: { RETRY: '#purchasePack.validating' },
},
},
},
{
guards: {
isRecaptchaEnabled: (context) => context?.flags?.purchaseRecaptcha,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment