Skip to content

Instantly share code, notes, and snippets.

@dangh
Last active July 30, 2019 07:11
Show Gist options
  • Save dangh/577f033b4283aaa69c1e11108bf962dd to your computer and use it in GitHub Desktop.
Save dangh/577f033b4283aaa69c1e11108bf962dd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
let booking = new Machine({
context: {
paymentState: 'new'
},
initial: 'new',
states: {
new: {
on: {
PAYMENT_NEW: 'paying',
PAYMENT_CHARING: 'paying',
PAYMENT_PENDING: 'paying',
PAYMENT_FAILED: 'failed',
PAYMENT_CANCELLED: 'cancelled',
PAYMENT_AUTHORIZED: { target: 'paid', actions: ['setPaymentAuthorized'] },
PAYMENT_CAPTURED: { target: 'paid', actions: ['setPaymentCaptured'] },
}
},
paying: {
on: {
PAYMENT_FAILED: 'failed',
PAYMENT_CANCELLED: 'cancelled',
PAYMENT_AUTHORIZED: { target: 'paid', actions: ['setPaymentAuthorized'] },
PAYMENT_CAPTURED: { target: 'paid', actions: ['setPaymentCaptured'] },
}
},
paid: {
initial: 'AGODA_PROCESSING',
states: {
HOTELBEDS_PROCESSING: {
on: {
HOTELBEDS_FAILURE: '#failed',
HOTELBEDS_SUCCESS: [
{ target: '#confirming', cond: 'paymentIsAuthorized' },
{ target: '#confirmed', cond: 'paymentIsCaptured' }
],
}
},
AGODA_PROCESSING: {
on: {
AGODA_FAILURE: '#failed',
AGODA_RECEIVED: '#pending'
}
}
}
},
pending: {
id: 'pending',
initial: 'AGODA_LISTENING',
states: {
AGODA_LISTENING: {
on: {
AGODA_FAILURE: '#failed',
AGODA_SUCCESS: [
{ target: '#confirming', cond: 'paymentIsAuthorized' },
{ target: '#confirmed', cond: 'paymentIsCaptured' }
]
}
}
}
},
confirming: {
id: 'confirming',
on: {
PAYMENT_CAPTURED: 'confirmed'
}
},
confirmed: {
id: 'confirmed',
type: 'final'
},
failed: {
id: 'failed',
type: 'final',
onEntry: 'notificationBookingFailed'
},
cancelled: {
type: 'final'
}
}
}, {
actions: {
setPaymentAuthorized: assign({ paymentState: () => 'authorized' }),
setPaymentCaptured: assign({ paymentState: () => 'captured' }),
},
guards: {
paymentIsAuthorized: ctx => ctx.paymentState == 'authorized',
paymentIsCaptured: ctx => ctx.paymentState == 'captured',
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment