Skip to content

Instantly share code, notes, and snippets.

@sesteva
Last active December 12, 2019 23:24
Show Gist options
  • Save sesteva/6ec6c6aada6712e6244ac0d643ba7936 to your computer and use it in GitHub Desktop.
Save sesteva/6ec6c6aada6712e6244ac0d643ba7936 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)
// split into Cart and Checkout
const cartActions = {
incrementCount: assign((ctx, evt)=> ({
count: ctx.count + 1
})),
decreaseCount: assign((ctx, evt)=> ({
count: ctx.count - 1
}))
}
const cartGuards= {
isEmpty: (ctx, evt) => ctx.count < 1
}
const cartmachine = Machine({
id: 'cart',
initial: 'empty',
context: {
count: 0
},
states: {
empty: {
on: {
ADD: {
target: 'unpaid.normal',
actions: ['incrementCount']
},
}
},
unpaid: {
id: 'unpaid',
on: {
'': {
target: '#cart.empty',
cond: 'isEmpty'
},
REMOVE: {
target: '.normal',
actions: 'decreaseCount',
},
ADD: {
target: '.normal',
actions: ['incrementCount']
}, //update count
PAY: '#cart.paying'
},
states: {
error: {},
normal: {}
}
},
paying:{
on: {
RESOLVE: 'paid',
REJECT: 'unpaid.error'
}
},
paid: {
}
}
},{
actions: cartActions,
guards: cartGuards
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment