Created
September 28, 2021 15:21
-
-
Save austinsamsel/e0ee8ce0c4bd2d022165148feae4cfae 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', | |
RESERVE_NFT: 'RESERVE_NFT', | |
TRANSACTION: 'TRANSACTION', | |
BUYER_ADDRESS: 'BUYER_ADDRESS', | |
POLLING: 'POLLING', | |
SUCCESS: 'SUCCESS', | |
ERROR: 'ERROR', | |
}; | |
const EVENTS = { | |
AUTHORIZE: 'AUTHORIZE', | |
} | |
const MAX_POLL_DURATION = 10000; | |
const definition = { | |
id: 'purchase', | |
initial: FS.IDLE, | |
context: { | |
FS, | |
EVENTS, | |
ix: undefined, | |
ixArgs: undefined, | |
error: undefined, | |
}, | |
states: { | |
[FS.IDLE]: {}, | |
[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.BUYER_ADDRESS, | |
}, | |
}, | |
}, | |
[FS.BUYER_ADDRESS]: { | |
invoke: { | |
src: 'getBuyerAddress', | |
onDone: { | |
target: FS.POLLING, | |
actions: 'updateBuyerAddress', | |
}, | |
onError: { | |
target: FS.ERROR, | |
actions: 'assignError', | |
}, | |
}, | |
}, | |
[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.RESERVE_NFT, | |
actions: 'assignInteraction', | |
cond: 'isReservingNFT', | |
}, | |
{ | |
target: FS.TRANSACTION, | |
actions: 'assignInteraction', | |
}, | |
], | |
}, | |
}; | |
const services = {}; | |
const guards = { | |
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