Skip to content

Instantly share code, notes, and snippets.

@i1skn
Last active December 15, 2020 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i1skn/58616d449b07008d9d24c0e532226f64 to your computer and use it in GitHub Desktop.
Save i1skn/58616d449b07008d9d24c0e532226f64 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 fetchMachine = Machine({
// id: 'fetch',
// initial: 'idle',
// context: {
// retries: 0
// },
// states: {
// idle: {
// on: {
// FETCH: 'loading'
// }
// },
// loading: {
// on: {
// RESOLVE: 'success',
// REJECT: 'failure'
// }
// },
// success: {
// type: 'final'
// },
// failure: {
// on: {
// RETRY: {
// target: 'loading',
// actions: assign({
// retries: (context, event) => context.retries + 1
// })
// }
// }
// }
// }
// });
const isPhoneNumberDetailsCached = (context) => {
return !!context.pepper && !!context.phoneHash
}
const shouldRetry = (context, event, { cond }) => {
console.log(cond, context.retries, cond.maxRetries, context.retries < cond.maxRetries)
return context.retries < cond.maxRetries
}
const fetchingPhoneNumberDetailsState = {
initial: 'loading',
states: {
loading: {
on: {
'': {
target: 'loaded',
cond: isPhoneNumberDetailsCached
},
SUCCESS: {
target: 'loaded',
actions: assign({
pepper: () => 'pepper',
phoneHash: () => 'phoneHash'
}),
},
FAILURE: [{
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
}),
cond: {
type: 'shouldRetry',
maxRetries: 3
}
},
{
target: 'loadingError'
}
]
}
},
loaded: {
on: {
'NEXT': {
target: '#main.tbd',
},
}
},
loadingError: {
type: 'final'
}
}
}
const fetchingFromChainState = {
initial: 'loading',
states: {
loading: {
on: {
'': {
target: 'loaded',
cond: isPhoneNumberDetailsCached
},
SUCCESS: {
target: 'loaded',
actions: assign({
pepper: () => 'pepper',
phoneHash: () => 'phoneHash'
}),
},
FAILURE: [{
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
}),
cond: (context, event) => context.retries < PEPPER_RETRY_COUNT
},{
target: 'loadingError',
cond: (context, event) => context.retries >= PEPPER_RETRY_COUNT
}]
}
},
loaded: {
on: {
'NEXT': {
target: '#main.fetchingFromChain',
},
}
},
loadingError: {
type: 'final'
}
}
};
const mainMachine = Machine({
id: 'main',
key: 'main',
initial: 'idle',
context: {
pepper: null,
phoneHash: null,
isVerified: false,
numAttestationsRemaining: null,
total: null,
completed: false,
retries: 0,
},
states: {
idle: {
on: {
FETCH: 'fetchingPhoneNumberDetails'
}
},
fetchingPhoneNumberDetails: {
...fetchingPhoneNumberDetailsState
},
fetchingFromChain: {
...fetchingFromChainState,
},
tbd: {
on: {
'': 'verified'
},
},
verified: {
type: 'final',
},
}
},{
guards:{
shouldRetry
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment