Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active July 13, 2021 15:42
Show Gist options
  • Save austinsamsel/6bbfe0187fbcf4032c03223d68b0fcb3 to your computer and use it in GitHub Desktop.
Save austinsamsel/6bbfe0187fbcf4032c03223d68b0fcb3 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',
CONNECT_WALLET: 'CONNECT_WALLET',
TRANSACTION: 'TRANSACTION',
POLLING: 'POLLING',
SUCCESS: 'SUCCESS',
ERROR: 'ERROR',
};
const MAX_POLL_DURATION = 10000;
const definition = {
id: 'purchase',
initial: FS.IDLE,
context: {
FS,
ix: undefined,
ixArgs: undefined,
error: undefined,
},
states: {
[FS.IDLE]: {},
[FS.CONNECT_WALLET]: {
invoke: {
src: 'fclAuth',
onDone: {
target: FS.TRANSACTION,
actions: 'updateInteraction',
},
onError: {
target: FS.ERROR,
},
},
},
[FS.TRANSACTION]: {
invoke: {
src: 'txMachine',
data: {
FS: () => txFS,
client: (context) => context.client,
ix: (context) => context.ix,
ixArgs: (context) => context.ixArgs,
isAuthorized: () => true,
},
onDone: {
target: FS.POLLING,
},
},
},
[FS.POLLING]: {
after: {
[MAX_POLL_DURATION]: {
actions: send('STOP', { to: 'poll' }),
},
},
invoke: {
id: 'poll',
src: 'pollPurchaseStatus',
onError: {
target: FS.ERROR,
actions: 'assignError',
},
},
on: {
POLL_ERROR: {
target: FS.ERROR,
actions: 'assignError',
},
POLL_SUCCESS: {
target: FS.SUCCESS,
},
},
},
[FS.SUCCESS]: {},
[FS.ERROR]: {},
},
on: {
authorize: [
{
target: FS.TRANSACTION,
actions: 'assignInteraction',
cond: 'isWalletConnected',
},
{
target: FS.CONNECT_WALLET,
actions: 'assignInteraction',
},
],
},
};
Machine(definition);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment