Skip to content

Instantly share code, notes, and snippets.

@timkendall
Created August 19, 2020 23:01
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 timkendall/5049dd09ceb6f50640a3b000b103b1eb to your computer and use it in GitHub Desktop.
Save timkendall/5049dd09ceb6f50640a3b000b103b1eb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const state = { currentBalance: { value: 0 } }
const closingStates = {
id: 'investment-closure',
type: 'parallel',
states: {
noFundingSource: {
initial: 'none',
states: {
none: {
on: {
CREATED: 'verify'
}
},
verify: {
on: {
VERIFIED: 'success'
}
},
success: {}
},
},
hasPendingInvestments: {
initial: 'pending',
states: {
pending: {
on: {
INVESTMENT_SETTLED: [
{ target: 'pending', cond: 'pendingInvestments' },
{ target: 'none' }
]
}
},
none: {}
}
},
hasRecurringInvestments: {
initial: 'on',
states: {
on: {
on: {
CANCEL_RECURRING: 'off'
},
},
off: {}
}
},
hasBalance: {
initial: 'balance',
states: {
balance: {
on: {
LIQUIDATE: [
{ target: 'pending', cond: 'hasFundingSource' },
{ target: '#investment-closure.noFundingSource' }
],
}
},
pending: {
on: {
DONE: 'success'
}
},
success: {}
}
}
}
}
const simulator = Machine(
{
id: 'investment',
context: state,
initial: 'open',
states: {
created: {
on: {
OPEN: 'opening',
}
},
opening: {},
open: {
on: {
CLOSE: [
{ target: 'closing', cond: 'fradulentUser' },
{ target: 'closing', cond: 'hasBalance' },
{ target: 'closing', cond: 'pendingInvestments' },
{ target: 'closing', cond: 'recurringInvestmentsSet' },
{ target: 'closed' }
]
}
},
closing: {
on: {
CANCEL: 'open',
},
...closingStates,
},
closed: {
on: {
REOPEN: 'opening',
}
},
},
},
{
guards: {
fradulentUser: () => false,
hasFundingSource: () => false,
hasBalance: (context) => context.currentBalance.value > 0,
pendingInvestments: () => true,
recurringInvestmentsSet: () => true,
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment