Last active
August 11, 2021 23:11
-
-
Save austinsamsel/b1b271ff34eaaa5a660f589dc6a3d21e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const FS = { | |
IDLE: 'IDLE', | |
CONNECT_WALLET: 'CONNECT_WALLET', | |
RESERVE_NFT: 'RESERVE_NFT', | |
TRANSACTION: 'TRANSACTION', | |
POLLING: 'POLLING', | |
SUCCESS: 'SUCCESS', | |
ERROR: 'ERROR', | |
}; | |
const EVENTS = { | |
AUTHORIZE: 'AUTHORIZE', | |
} | |
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.RESERVE_NFT, | |
actions: 'updateBuyerAddress', | |
cond: 'isReservingNFT', | |
}, | |
{ | |
target: FS.TRANSACTION, | |
actions: 'updateBuyerAddress', | |
}, | |
], | |
onError: { | |
target: FS.ERROR, | |
actions: 'assignError', | |
}, | |
}, | |
}, | |
[FS.RESERVE_NFT]: { | |
invoke: { | |
src: 'reserveNFT', | |
onDone: { | |
target: FS.TRANSACTION, | |
actions: 'updateNFTinfo', | |
}, | |
onError: { | |
target: FS.ERROR, | |
actions: 'assignError', | |
}, | |
}, | |
}, | |
[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: { | |
[EVENTS.AUTHORIZE]: [ | |
{ | |
target: FS.CONNECT_WALLET, | |
actions: 'assignInteraction', | |
cond: 'isConnectingWallet', | |
}, | |
{ | |
target: FS.RESERVE_NFT, | |
actions: 'assignInteraction', | |
cond: 'isReservingNFT', | |
}, | |
{ | |
target: FS.TRANSACTION, | |
actions: 'assignInteraction', | |
}, | |
], | |
}, | |
}; | |
const services = {}; | |
const guards = { | |
isConnectingWallet: () => true, | |
isReservingNFT: () => true, | |
}; | |
Machine(definition, { | |
actions, | |
services, | |
guards, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment