Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active May 5, 2021 19:02
Show Gist options
  • Save austinsamsel/d487260271b0fbf492e12e405ba269d6 to your computer and use it in GitHub Desktop.
Save austinsamsel/d487260271b0fbf492e12e405ba269d6 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 MAX_POLL_DURATION = 1000 * 60;
const FS = {
IDLE: 'idle',
AUTHORIZING: 'authorizing',
SEALING: 'sealing',
SEALED: 'sealed',
POLLING: 'polling',
DONE: 'done',
ERROR: 'error',
};
const machineDef = {
id: 'transaction',
initial: FS.IDLE,
context: {
ix: undefined,
txId: undefined,
txStatus: undefined,
error: undefined,
},
states: {
[FS.IDLE]: {},
[FS.AUTHORIZING]: {
entry: 'reset',
invoke: {
src: 'fclSend',
onDone: {
target: 'sealing',
actions: 'assignTxId',
},
onError: {
target: '#error',
actions: 'assignError',
},
},
},
[FS.SEALING]: {
invoke: {
src: 'fclOnceSealed',
onDone: [
{
target: 'polling',
actions: 'assignTxStatus',
cond: 'isPollingEnabled',
},
{
target: 'sealed',
actions: 'assignTxStatus',
},
],
onError: {
target: '#error',
actions: 'assignError',
},
},
},
[FS.SEALED]: {
id: FS.SEALED,
},
[FS.POLLING]: {
after: {
[MAX_POLL_DURATION]: {
actions: send('STOP', { to: 'poll' }),
},
},
invoke: {
id: 'poll',
src: 'pollService',
onError: {
target: '#transaction.error',
// actions: 'setErrorMessage',
},
},
on: {
POLL_ERROR: {
target: '#transaction.error',
// actions: 'setErrorMessage',
},
POLL_SUCCESS: {
target: '#transaction.done',
},
},
},
[FS.DONE]: {
id: FS.DONE,
},
declined: {},
[FS.ERROR]: {
id: FS.ERROR,
},
},
on: {
authorize: {
target: 'authorizing',
actions: 'assignAuthorizeData',
},
},
};
const services = {
fclSend: () => {},
fclOnceSealed: () => {},
};
const guards = {
isReceived: (ctx, event) => false,
isPollingEnabled: (ctx) => ctx?.isPollingEnabled,
};
const machine = Machine(machineDef,
{
actions,
services,
guards,
delays: { MAX_POLL_DURATION },
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment