Created
August 19, 2020 23:01
-
-
Save timkendall/5049dd09ceb6f50640a3b000b103b1eb 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
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