Last active
July 30, 2019 07:11
-
-
Save dangh/577f033b4283aaa69c1e11108bf962dd 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
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